English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C# supports up to32A multidimensional array with dimensions. Multidimensional arrays can be declared by adding commas in brackets. For example, [ , ] declares a two-dimensional array, [ , , ] declares a three-dimensional array, [ , , , ] declares a four-dimensional array, and so on. So, in multidimensional arrays, the number of commas = the number of dimensions-1.
int[, ,] arr2d; // Two-dimensional array int[, ,] arr3d; // Three-dimensional array int[, , ] arr4d ; // Four-dimensional array int[, , , ,] arr5d; // Five-dimensional array
Let's understand two-dimensional arrays. Below initializes a two-dimensional array.
int[, ,] arr2d = new int[3,2]{ {1, 2}, {3, 4}, {5, 6} }; //or int[, ,] arr2d = { {1, 2}, {3, 4}, {5, 6} };
In the above example of a two-dimensional array, [3,2] defines the number of rows and columns. The first column represents the number of rows, and the second column defines the number of columns. The following diagram demonstrates a two-dimensional array divided into rows and columns.
The following access values of a two-dimensional array.
int[, ,] arr2d = new int[3,2]{ {1, 2}, {3, 4}, {5, 6} }; arr2d[0, 0]; //Returns1 arr2d[0, 1]; //Returns2 arr2d[1, 0]; //Returns3 arr2d[1, 1]; //Returns4 arr2d[2, 0]; //Returns5 arr2d[2, 1]; //Returns6 //arr2d[3, 0]; //throws a runtime error because there is no fourth row
In the above example, the value of the two-dimensional array can be accessed through the row and column index numbers [row index, column index]. Therefore, [0, 0] returns the first row and first column [1, 1] value, and return the value of the second row and second column.
Now, let's understand three-dimensional arrays. The following statement initializes a three-dimensional array.
int[, ,] arr3d1 = new int[1, 2, 2]{ { { 1, 2} 3, 4} }; int[, ,] arr3d2 = new int[2, 2, 2]{ { {1, 2}3, 4} }, { {5, 6}7, 8} }; int[, ,] arr3d3 = new int[2, 2, 3]{ { { 1, 2, 3}4, 5, 6} }, { { 7, 8, 9}10, 11, 12} }; arr3d2[0, 0, 0]; // Returns1 arr3d2[0, 0, 1]; // Returns2 arr3d2[0, 1, 0]; // Returns3 arr3d2[0, 1, 1]; // Returns4 arr3d2[1, 0, 0]; // Returns5 arr3d2[1, 0, 1]; // Returns6 arr3d2[1, 1, 0]; // Returns7 arr3d2[1, 1, 1]; // Returns8
As you can see in the example above, arr3d1[1,2,2] specifies that it will contain a two-dimensional array [2,2] one row. arr3d2Specify the dimension [2,2,2indicates that it contains a two-dimensional array [2,2The two lines. Therefore, the first column represents the number of rows in the internal two-dimensional array.
Now, see the following four-dimensional array.
int[,,,] arr4d1 = new int[1, 1, 2, 2]{ { { { 1, 2} 3, 4} } }; arr4d1[0, 0, 0, 0]; // Returns1 arr4d1[0, 0, 0, 1]; // Returns2 arr4d1[0, 0, 1, 0]; // Returns3 arr4d1[0, 0, 1, 1]; // Returns4 int[,,,] arr4d2 = new int[1, 2, 2, 2]{ { { {1, 2}3, 4} }, { {5, 6}7, 8} } }; arr4d2[0, 0, 0, 0]; // Returns1 arr4d2[0, 0, 0, 1]; // Returns2 arr4d2[0, 0, 1, 0]; // Returns3 arr4d2[0, 0, 1, 1]; // Returns4 arr4d2[0, 1, 0, 0]; // Returns5 arr4d2[0, 1, 0, 1]; // Returns6 arr4d2[0, 1, 1, 0]; // Returns7 arr4d2[0, 1, 1, 1]; // Returns8
In the above example, the four-dimensional array arr4d1Specify [1,1,2,2], which indicates that it contains a row of three-dimensional arrays.
In the same way, you can declare and initialize5Dimension,6Dimensional array and at most32Dimensional array.