05 Feb C# Multi-Dimensional Arrays
In the last lesson we learned about One-Dimensional Arrays in Java and how to use them. Now, we will see another type of arrays in C# i.e. Multidimensional Arrays. Multi-dimensional arrays as the name suggests are arrays of arrays. These are also called rectangular arrays. Multi-Dimensional can be the following in C#:
- Two-Dimensional Arrays
- Three-Dimensional Arrays
Declare and Initialize a Multi-Dimensional Array
To create a Multi-Dimensional array, set a comma inside the square bracket. You need to use two square brackets to declare a multidimensional array.
For a Two-Dimensional array, set a single comma as shown below:
1 2 3 |
int[,] marks = new int[2,2]; |
We can also directly declare and initialize a Two-Dimensional array:
1 2 3 4 5 6 7 |
int [,] marks = new int [3,3] { {90, 75, 86} , {72, 89, 68} , {58, 99, 93} }; |
For a Three-Dimensional array, set two comma’s as shown below:
1 2 3 |
int[,,] marks = new int[2,2,2]; |
The following figure displays how a multidimensional array forms a 2×3 matrix,
In the above figure, you can see the left index determines row and the right index determines column.
Example – Multi-Dimensional Arrays in C#
Let us now see an example to display each element from our 2×3 matrix,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System; namespace Demo { class Studyopedia { static void Main(string[] args) { int i, j, k = 0; int[,] marks = new int[2,3]; for(i=0; i<2; i++) { for(j=0; j<3; j++) { marks[i, j] = k; k++; } } for(i=0; i<2; i++) { for(j=0; j<3; j++) Console.WriteLine("marks[{0},{1}] = {2}", i, j, marks[i,j]); Console.Write("\n"); } } } } |
Output
1 2 3 4 5 6 7 8 9 |
marks[0,0] = 0 marks[0,1] = 1 marks[0,2] = 2 marks[1,0] = 3 marks[1,1] = 4 marks[1,2] = 5 |
Example – Access elements of a Multi-Dimensional Array in C#
We can easily access the elements of a two-dimensional array. Set the array name and then two indexes i.e.
1 2 3 4 5 6 7 8 9 10 |
// Access the 1st row and 1st column marks[0,0] // Access the 1st row and 2nd column marks[0,1] // Access the 2nd row and 2nd column marks[1,1] |
Let us follow what we saw above in the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System; namespace Demo { class Studyopedia { static void Main(string[] args) { int i, j, k = 0; int [,] marks = new int [3,3] { {90, 75, 86} , {72, 89, 68} , {58, 99, 93} }; // 1st row elements Console.WriteLine("Access the 1st row and 1st column = {0}",marks[0,0]); Console.WriteLine("Access the 1st row and 2nd column = {0}",marks[0,1]); Console.WriteLine("Access the 1st row and 3rd column = {0}",marks[0,2]); // 2nd row elements Console.WriteLine("\nAccess the 2nd row and 1st column = {0}",marks[1,0]); Console.WriteLine("Access the 2nd row and 2nd column = {0}",marks[1,1]); Console.WriteLine("Access the 2nd row and 3rd column = {0}",marks[1,2]); // 3rd row elements Console.WriteLine("\nAccess the 3rd row and 1st column = {0}",marks[2,0]); Console.WriteLine("Access the 3rd row and 2nd column = {0}",marks[2,1]); Console.WriteLine("Access the 3rd row and 3rd column = {0}",marks[2,2]); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Access the 1st row and 1st column = 90 Access the 1st row and 2nd column = 75 Access the 1st row and 3rd column = 86 Access the 2nd row and 1st column = 72 Access the 2nd row and 2nd column = 89 Access the 2nd row and 3rd column = 68 Access the 3rd row and 1st column = 58 Access the 3rd row and 2nd column = 99 Access the 3rd row and 3rd column = 93 |
No Comments