An array is a data structure that can contain multiple elements of the same type, and array elements in C # start from zero.
Arrays can be divided into: simple arrays, multidimensional arrays, and jagged arrays.
Simple array
1. Declaration and initialization
int [] x = new INT[5];
Once an array has been declared, it must be allocated an array of memory to hold the elements of the arrays. An array is a reference type, so it must be allocated a push memory. Initializes the array's variables using the new operator, specifying the type and number of elements in the array.
1. Use an array initializer to assign values to each element of a group
int [] x = new int[5]{10,6,7,15,8};
2. Do not specify the size of the array
int [] x = new int[]{10,6,7,15,8};
3. Simplified form
int [] x = {10,6,7,15,8};
2. Accessing array elements
1. Using indexers to pass element numbers to access an array
int [] x = {10,6,7,15,8}; Console.WriteLine (X[0]); 10console.writeline (x[1]); 6
2. Use for loop traversal
int[] x = {Ten, 6, 7, 8};for (int i = 0; i < x.length; i++) { Console.Write (x[i]+ ""); 10 6 7 15 8}
3. Traversing with a foreach loop
int[] x = {Ten, 6, 7, 8};foreach (var i in x) { Console.Write (i + ""); 10 6 7 15 8}
4. Sorting using Array.Sort
int[] x = {10, 6, 7, 15, 8}; Array.Sort (x); foreach (var i in x) { Console.Write (i + ""); 6 7 8 10 15}
5. Classic title: Bubble sort
Int[] Array = {Ten, 6, 7, 8};int temp = 0;for (int i = 0; i < array. Length-1; i++) {for (int j = i + 1; j < Array. Length; J + +) { if (Array[j] < Array[i]) { temp = array[i]; Array[i] = array[j]; ARRAY[J] = temp;}} } foreach (var i in array) { Console.Write (i + ""); 6 7 8 10 15}
Multidimensional arrays
Multidimensional arrays are indexed by two or more integers.
1. Two-d array declaration and initialization
int[,] y1 = new int[2, 2];y[0, 0] = 1;y[0, 1] = 2;y[1, 0] = 3;y[1, 1] = 4;
int[,] y2 = {{1, 2}, {3, 4}};
2. Three-dimensional arrays
Int[,,] y3 ={ {{1, 2}, {3, 4}}, {{5, 6}, { 7, 8}}, { {9, 10}, {11, 12}},};
Jagged array
A two-dimensional array is a complete rectangle, and the jagged array is more flexible than the large size, and in the jagged array, each row can have a different size.
Int[][] J = new Int[3][];j[0] = new Int[2] {1, 2};j[1] = new Int[5] {3, 4, 5, 6, 7};j[2] = new Int[3] {8, 9, 10};
C # arrays