What is an array?
An array is a data structure that contains multiple elements of the same type.
Declaration of array:
int[] myIntArray;
Note: When declaring an array, square brackets ([]) must follow the type, not the variable name. In C#, it is illegal syntax to put square brackets after a variable name.
Initialization of array:
We know that array is a reference type, so we need to allocate memory on the heap to it.
1.myIntArray = new int[3];
2.myIntArray = new int[] { 1, 2, 3 };
3.int[] myIntArray = { 1, 2, 3 }; //When using this When the method initializes the array, it can only be used when declaring the variable array, not after declaring the array.
Array access:
After the array is declared and initialized, it can be accessed using an indexer. The indexer always starts with 0, indicating the first element.
int[] myIntArray = { 1, 2, 3 }; Console.WriteLine("intValue = {0}", myIntArray[0]); Console.Read();
The result is: intValue = 1
Type of array:
1. Multi-dimensional array:
General arrays (also called one-dimensional arrays) are indexed by an integer, and multi-dimensional arrays are indexed by two or more integers.
static void Main(string[] args) { //声明一个二维数组 两行三列 int[,] myIntArray1; myIntArray1 = new int[2, 3]; myIntArray1[0, 0] = 1; myIntArray1[0, 1] = 11; myIntArray1[0, 2] = 111; myIntArray1[1, 0] = 2; myIntArray1[1, 1] = 22; myIntArray1[1, 2] = 222; Console.WriteLine("{0},{1},{2}", myIntArray1[0, 0], myIntArray1[0, 1], myIntArray1[0, 2]); Console.WriteLine("{0},{1},{2}", myIntArray1[1, 0], myIntArray1[1, 1], myIntArray1[1, 2]); Console.Read(); }
The result is:
static void Main(string[] args) { //声明一个二维数组 三行三列 int[,] myIntArray2; myIntArray2 = new int[,] { { 1, 11, 111 }, { 2, 22, 222 }, { 3, 33, 333 }, { 4, 44, 444 } }; Console.WriteLine("{0},{1},{2}", myIntArray2[0, 0], myIntArray2[0, 1], myIntArray2[0, 2]); Console.WriteLine("{0},{1},{2}", myIntArray2[1, 0], myIntArray2[1, 1], myIntArray2[1, 2]); Console.WriteLine("{0},{1},{2}", myIntArray2[2, 0], myIntArray2[2, 1], myIntArray2[2, 2]); Console.WriteLine("{0},{1},{2}", myIntArray2[3, 0], myIntArray2[3, 1], myIntArray2[3, 2]); Console.Read(); }
The result is:
static void Main(string[] args) { //声明一个三维数组 三行三列 int[, ,] myIntArray3; myIntArray3 = new int[,,] { { {1,1}, {11,11}, {111,111} }, { {2,2}, {22,22}, {222,222} }, { {3,3}, {33,33}, {333,333} }, { {4,4}, {44,44}, {444,444} } }; Console.WriteLine("{0},{1},{2},{3},{4},{5}", myIntArray3[0, 0, 0], myIntArray3[0, 0, 1], myIntArray3[0, 1, 0], myIntArray3[0, 1, 1], myIntArray3[0, 2, 0], myIntArray3[0, 2, 1]); Console.WriteLine("{0},{1},{2},{3},{4},{5}", myIntArray3[1, 0, 0], myIntArray3[1, 0, 1], myIntArray3[1, 1, 0], myIntArray3[1, 1, 1], myIntArray3[1, 2, 0], myIntArray3[1, 2, 1]); Console.WriteLine("{0},{1},{2},{3},{4},{5}", myIntArray3[2, 0, 0], myIntArray3[2, 0, 1], myIntArray3[2, 1, 0], myIntArray3[2, 1, 1], myIntArray3[2, 2, 0], myIntArray3[2, 2, 1]); Console.WriteLine("{0},{1},{2},{3},{4},{5}", myIntArray3[3, 0, 0], myIntArray3[3, 0, 1], myIntArray3[3, 1, 0], myIntArray3[3, 1, 1], myIntArray3[3, 2, 0], myIntArray3[3, 2, 1]); Console.Read(); }
The result is:
2. Sawtooth array:
Generally, the size of multi-dimensional arrays is rectangular, and the size of sawtooth arrays is relatively small Flexible, each row can be a different size.
When initializing the sawtooth array, first set the number of rows contained in the array. The second bracket defining the number of elements in each row is set to empty because each row of such an array contains a different number of elements.
static void Main(string[] args) { //声明一个锯齿数组 三行 int[][] myIntArray4; myIntArray4 = new int[3][]; myIntArray4[0] = new int[] { 1,11,111}; myIntArray4[1] = new int[2] { 2, 22 }; myIntArray4[2] = new int[] { 3,33,333,3333}; for (int i = 0; i < myIntArray4.Length; i++) { for (int j = 0; j < myIntArray4[i].Length; j++) { Console.WriteLine("{0}",myIntArray4[i][j]); } } Console.Read(); }
The result is:
Array in C#
For more articles related to array in C#, please pay attention to the PHP Chinese website!