Sort an Array in C# in Descending Order

karthikeya Boyini
Updated on 23-Jun-2020 14:46:40

5K+ Views

The following is the unsorted array.int[] list = {98, 23, 97, 36, 77};Now first use the Sort() method to sort the array.Array.Reverse(list);Use the Reverse() method that would eventually give you a sorted array in descending order.Array.Reverse(list);You can try to run the following code to to sort an array in descending order.Example Live Demousing System; namespace Demo {    public class MyApplication {       public static void Main(string[] args) {          int[] list = {98, 23, 97, 36, 77};          Console.WriteLine("Original Unsorted List");          foreach (int i in list) {   ... Read More

Match All Occurrences of a Regex in Java

Arnab Chakraborty
Updated on 23-Jun-2020 14:46:06

298 Views

public class RegexOccur {    public static void main(String args[]) {       String str = "java is fun so learn java";       String findStr = "java";       int lastIndex = 0;       int count = 0;       while(lastIndex != -1) {          lastIndex = str.indexOf(findStr,lastIndex);          if(lastIndex != -1) {             count ++;             lastIndex += findStr.length();          }       }       System.out.println(count);    } }Output2

Use Multi-Dimensional Arrays in C#

karthikeya Boyini
Updated on 23-Jun-2020 14:45:08

253 Views

C# allows multidimensional arrays. Multi-dimensional arrays are also called rectangular array. Declare a 2-dimensional array of strings as.string [, ] names;A 2-dimensional array can be thought of as a table, which has x number of rows and y number of columns.Multidimensional arrays may be initialized by specifying bracketed values for each row. The following array is with 4 rows and each row has 4 columns.int [, ] a = new int [4, 4] {    {0, 1, 2, 3} , /* initializers for row indexed by 0 */    {4, 5, 6, 7} , /* initializers for row indexed by ... Read More

Replace Backward Slash from a String

Malhar Lathkar
Updated on 23-Jun-2020 14:44:01

695 Views

In Python it does give the desired result>>> var  = "aaa\bbb\ccc and ddd\eee" >>> var.split('\') ['aaa', 'bbb', 'ccc and ddd', 'eee']

Prevent Reflection from Breaking a Singleton Class Pattern

Chandu yadav
Updated on 23-Jun-2020 14:43:37

938 Views

A Singleton pattern states that a class can have a single instance and multiple instances are not permitted to be created. For this purpose, we make the constructor of the class a private and return a instance via a static method. But using reflection, we can still create multiple instance of a class by modifying the constructor scope. See the example below −Example - Breaking Singleton Live Demoimport java.io.Serializable; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; public class Tester {    public static void main(String[] args) throws    InstantiationException, IllegalAccessException,    IllegalArgumentException, InvocationTargetException{       A a = A.getInstance();     ... Read More

Sort Two-Dimensional Array in C#

karthikeya Boyini
Updated on 23-Jun-2020 14:41:25

2K+ Views

To sort a two-dimensional array in C#, in a nested for loop, add another for loop to check the following condition.Examplefor (int k = 0; k < j; k++) {    if (arr[i, k] > arr[i, k + 1]) {       int myTemp = arr[i, k];       arr[i, k] = arr[i, k + 1];       arr[i, k + 1] = myTemp;    } }Till the outer loop loops through, use the GetLength() method as shown below. This is done to sort the array.Examplefor (int i = 0; i < arr.GetLength(0); i++) {    for ... Read More

Use foreach Statement for Accessing Array Elements in C#

karthikeya Boyini
Updated on 23-Jun-2020 14:39:01

172 Views

To access Array elements in a foreach statement, use the numeric index.Let’s say the following is our code.Example Live Demousing System; namespace ArrayApplication {    class MyArray {       static void Main(string[] args) {          int [] n = new int[10]; /* n is an array of 10 integers */          /* initialize elements of array n */          for ( int i = 0; i < 10; i++ ) {             n[i] = i + 100;          }       ... Read More

Prevent Serialization from Breaking Singleton Class Pattern

Arjun Thakur
Updated on 23-Jun-2020 14:38:06

715 Views

A Singleton pattern states that a class can have a single instance and multiple instances are not permitted to be created. For this purpose, we make the constructor of the class a private and return a instance via a static method. But using serialization, we can still create multiple instance of a class. See the example below −Example - Breaking Singleton Live Demoimport java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class Tester{    public static void main(String[] args)    throws ClassNotFoundException, IOException{       A a = A.getInstance();       A b ... Read More

Using For Loop to Access Array Elements in C#

Samual Sam
Updated on 23-Jun-2020 14:35:45

326 Views

The ‘for loop’ executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.The following is our for loop.Example Live Demousing System; namespace ArrayApplication {    class MyArray {       static void Main(string[] args) {          int [] n = new int[10]; /* n is an array of 10 integers */          int i, j;          /* initialize elements of array n */          for ( i = 0; i < 10; i++ ) {             n[ ... Read More

Pass Parameters by Value in a C# Method

karthikeya Boyini
Updated on 23-Jun-2020 14:32:38

176 Views

This is the default mechanism for passing parameters to a method. In this mechanism, when a method is called, a new storage location is created for each value parameter.The values of the actual parameters are copied into them. Hence, the changes made to the parameter inside the method have no effect on the argument. The following is the code to pass parameters by value.Example Live Demousing System; namespace CalculatorApplication {    class NumberManipulator {       public void swap(int x, int y) {          int temp;          temp = x; /* save the value ... Read More

Advertisements