Learn the C + + language array, and then learn the C # language array, always think of the difference between them, define the format, writing form, the assignment form is different. In contrast to the C + + language, the C # language provides an array that is more practical, and the C # language array can be dynamically assigned if the element value is not known.
one-dimensional arrays
declaration format for one-dimensional arrays:
data type [] < array name >
There are two types of initialization of one-dimensional arrays:
(1), int[] array={1,2,3,4,5,6}
(2), int[] Array=new int[6] {1,2,3,4,5,6}
or int[] Array; Array=new int[6] {1,2,3,4,5,6}
Example: changing an element in an array according to an index and outputting all elements of the array after the change
<span style= "FONT-SIZE:18PX;" >using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace consoleapplication1{ class program { static void Main (string[] args) { int[] Array; Array = new int[6]{1,2,3,4,5,6}; Console.WriteLine ("The second element of an Array: {0}", array[1]); ARRAY[1] = 7; Console.WriteLine ("The second person element of the changed Array: {0}", array[1]); Console.WriteLine ("The following are all elements of the changed array"); foreach (int temp in Array) { Console.WriteLine (temp); } Console.ReadLine ();}}} </span>
The result of the output is clearly known as:the second element of the array: 2
The second person element of the changed array: 7
The following are all elements of the changed array 1 7 3 4 5 6 (output of one element per line)
two-dimensional arrays
the declaration format for a two-dimensional array is:
data type [,] < array name >
Two-dimensional arrays are initialized in the form of:
(1), int[,] array={, (3,4), (5,6)}hu
(2), int[,] array=new int[3,2] {(), (3,4), (5,6)}
Or int[,] Array; Array=new int[3,2] {($), (3,4), (5,6)}
example, changing the elements in an array according to the index and outputting the elements after the changed array
<span style= "FONT-SIZE:18PX;" >using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace consoleapplication1{ class program { static void Main (string[] args) { int[,] Array; Array=new int[3,2]{{1,2},{3,4},{5,6}}; Console.WriteLine ("Array element of the preceding array is:"); foreach (int temp in Array) { Console.WriteLine (temp); } array[1, 0] = 7; Array[1, 1] = 8; Console.WriteLine ("The array element after the change is:"); foreach (int item in Array) { Console.WriteLine (item); } Console.ReadLine ();}}} </span>
The result of the output is simple: the array element of the previous array is changed to: 1 2 3 4 5 6
the array elements of the changed array are: 1 2 7 8 5 6 (output of one element per line)
Array Sorting
array provides the sort method for sorting, but it is often used in conjunction with the reverse method, which reverses the order of the elements in the array.
The Sort method, which accepts an array, implements it in ascending order, in the format: Array.Sort (Array)
The Reverse method, which takes an array, reverses the order of the elements in the array, in the format: Array.reverse (Array)
example, the following array is ascending and then descending
<span style= "FONT-SIZE:18PX;" >using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace consoleapplication1{ class program { static void Main (string[] args) { int[] b; B=new int[6]{2,1,4,5,3,6}; Array.Sort (b); Console.WriteLine ("Ascending Order:"); foreach (int temp in B) { Console.WriteLine (temp); } Array.reverse (b); Console.WriteLine ("Descending order:"); foreach (int item in B) { Console.WriteLine (item); } Console.ReadLine ();}}} </span>
The output is simple: in ascending order: 1 2 3 4 5 6
Descending order: 6 5 4 3 2 1 (output one element per line)
number combination and split with
Array provides a copy method to implement the merging and splitting of the array. The Copy method has four overloads, namely:
(1) Array.copy (array 1, array 2, length) Int32
(2) array.copy (array 1, array 2, length) Int64
(3) array.copy (array 1, specified index, array 2, specified index, length) Int32
(4) array.copy (array 1, specified index, array 2, specified index, length) Int64
example, implementing merging arrays 1 and Arrays 2 array 3, splitting array 2 to array 4
<span style= "font-size:18px;" >using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace consoleapplication1{class Program {static void Main (string[] args) {int[] arr1 = {1,2,3,4}; Int[] arr2 = {5,6,7,8,9}; Int[] Arr3=new int[9]; Int[] Arr4=new int[3];//takes the first three elements of the array 2 array.copy (arr1, 0, ARR3, 0, 4);//merge array 1 to array 3 array.copy (arr2, 0, ARR3, 4, 5);//merge array 2 to array 3 array.copy (arr2, 0, ARR4, 0, 3);//Split fraction Group 2 first three elements to array 4 Console.WriteLine ("Merging the elements of the arrays 3 (The Vegetarian is: "); foreach (int temp in ARR3) {Console.WriteLine (temp); } Console.WriteLine ("Split the element into array 4 is:"); foreach (int item in ARR4) {Console.WriteLine (item); } console.readline (); }}}</span>
The result of the output is: merging the elements of the array 3 to: 1 2 3 4 5 6 7 8 9
The elements that are split into array 4 are: 5 6 7
Array of C # supplements