(A) Rectangular array
In C#, rectangular arrays, also known as multi-dimensional arrays, are arrays that use multiple indexes to access their elements.
1. Declaration of two-dimensional arrays:
<baseType>[,] <name>;
For example, declare and initialize a two-dimensional array doubleArray, whose basic type is double, 3 rows and 4 columns:
double[,] doubleArray=new double[3,4]; double[,] doubleArray={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
Of course, we can also define and initialize doubleArray like this:
double[,] doubleArray=new double[3,4]{{1,2,3,4},{5,6,7,8},{9,10,11,12}};
2. Access elements in the array according to the index
Access array elements with row number 1 and column number 2 (row 2, column 3) in the array:
(doubleArray[1,2]);//Console output 7
3. Traverse a two-dimensional array
(1) Get the dimension of a multi-dimensional array
int rank=;//rank=2, indicating that the dimension of the doubleArray array is 2
(2) For loop traversal
// For loop through doubleArray arrayfor(int i=0;i<(0);i++)//GetLength(0) gets the number of array rows{ for(int j=0;j<(1);j++)//GetLength(1) Get the number of array columns { (doubleArray[i,j]+"\t");//Print array elements with index [i,j] } ();//After printing, line break}
(3) Foreach traversal
//Foreach traverses doubleArray arrayforeach(double item in doubleArray) { (item + "\t");//Output the elements in the doubleArray array in turn}
4. Other multidimensional arrays
Other multidimensional arrays need more commas separated, declared format:
<baseType>[,,] <name>;// 3D array<baseType>[,,,] <name>;//Four-dimensional array<baseType>[,,,,] <name>;// Five-dimensional array...
(II) Sawtoothed array
The above rectangular array requires that the number of elements in each row of the array is the same, but for a jagged array, it can make the number of elements in each row of the array different.
Jagged arrays are also called interleaved arrays. Each element in the array is another array. All jigshot arrays are also called arrays of arrays, but be careful that these arrays must have the same basic type.
1. Declaration and initialization of jagged arrays
(1) Sawtooth array declaration
int[][] jaggedArray;//Declare an int-type jaggedArray
(2) Initialization of the sawtooth array
Initialize the jaggedArray array with 2 subarrays (that is, 2 elements in the value are another 2 arrays)
jaggedArray = new int[2][];//Initialize the jaggedArray arrayjaggedArray[0] = new int[3] { 1, 2, 3 };//The first element is a one-dimensional array of length 3jaggedArray[1] = new int[4] { 4, 5, 6, 7 };//The second element is a one-dimensional array of length 4
You can also improve the declaration and initialization of the jagged array, writing the declaration and initialization in one line of code:
int[][] jaggedArray = new int[2][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6, 7 } };
2. Iterate through the jagged array
(1) For loop traversal
// For loop traverses the jaggedArray arrayfor(int i = 0; i < ; i++)//Transfer the number of rows in the array{ for (int j = 0; j < jaggedArray[i].Length; j++)//Iterate over the elements of each row in the array (one-dimensional array) { (jaggedArray[i][j]+"\t");//Print jaggedArray[i][j] } ();//One line-win}
(2) Foreach traversal
//Foreach traversal of jaggedArray arrayforeach(int[] int_item in jaggedArray)//Transfer each subarray in the jagged number, type int[]{ foreach(int item in int_item)//Iterate through the elements in each subarray, type int { (item + "\t");//Output elements in the jagged array in turn } }
Summarize
The syntax of using jagged arrays is much more complicated than rectangular arrays, whether it is the declaration and initialization of arrays or the traversal of arrays. In most cases, using rectangular arrays is simpler, and it is also a relatively simple way to store them. However, sometimes, serrated arrays must be used, and the efficiency will not be reduced as a result.
This is the article about the implementation of rectangular arrays (multi-dimensional arrays) and jagged arrays in C#. For more related C# rectangular arrays and jagged arrays, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!