In mathematics, one-dimensional arrays are also known as vectors. Some of you may still remember (riiight! 🙂) that multidimensional arrays are called matrices. In programming, we call multidimensional arrays any array with more than one dimension.
For instance, how would we represent the structure of a chess board programmatically? It has two dimensions: one of numbers (starting from one, ending with 8) and one of letters (from A to H), and we identify a cell by the two combined, like in a system of coordinates: H3, B6, etc.
The way we achieve this programatically is by declaring a two dimensional array, like this:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
using
System;
namespace
HelloWorld
{
class
Program
{
static void Main(string[] args)
{
int[,] twoDimensionalArray;
Console.ReadLine();
}
}
}
|
If you want to represent a three dimensional system, like the coordinates of a 3D point (X, Y and Z axis) you can declare a three-dimensional array:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
using
System;
namespace
HelloWorld
{
class
Program
{
static void Main(string[] args)
{
int[,,] threeDimensionalArray;
Console.ReadLine();
}
}
}
|
and so on. However, two dimensional arrays are rarely used, and more than two dimensions even more rarely. However, in theory, you could declare an infinite number of dimensions.
We declare multidimensional array like so:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
using
System;
namespace
HelloWorld
{
class
Program
{
static void Main(string[] args)
{
int[,] intMatrix;
float[,] floatMatrix;
string[,,] strCube;
Console.ReadLine();
}
}
}
|
and we can initialize them like this:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
using
System;
namespace
HelloWorld
{
class
Program
{
static void Main(string[] args)
{
int[,] intMatrix = new int[3, 4];
float[,] floatMatrix = new float[8, 2];
string[,,] strCube = new string[5, 5, 5];
Console.ReadLine();
}
}
}
|
Because multidimensional arrays are hard to understand as a concept seen like that, so you can better imagine and visualize them by the following image:
The above image represents the visual representation of our intMatrix array, populated with some random elements. As you can see from our initialization, we declared intMatrix with two dimensions, one of 3 elements, one of 4. The image illustrates the same exact thing, keeping into account that multidimensional arrays, just like regular arrays, are 0 based indexed.
Lastly, this is how we initialize our multidimensional arrays with some values:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
using
System;
namespace
HelloWorld
{
class
Program
{
static void Main(string[] args)
{
int[,] matrix =
{
{ 1, 2, 3, 4 }, // row 0 values, with 4 columns
{ 5, 6, 7, 8 }, // row 1 values, with 4 columns
};
// the matrix size is 2 x 4 (2 rows, 4 columns)
Console.ReadLine();
}
}
}
|
And yes, it is a bit complicated. But don’t feel bad, even experienced programmers have problems conceptualizing more than three dimensional arrays 😀
However, accessing or setting the value of specific locations of multidimensional arrays is a bit more simpler. Lets consider the declaration from above. The array matrix has a total of 8 elements, 2 rows with 4 columns each. So, its elements can be accessed like this:
|
1
2
|
matrix[0, 0], matrix[0, 1], matrix[0, 2], matrix[0, 3] // first row
matrix[1, 0], matrix[1, 1], matrix[1, 2], matrix[1, 3] // second row
|
After a quick inspection of my example, you will probably figure out that two dimensional arrays can be accessed by
|
1
|
matrix[row, column]
|
and similarly, any number of dimensions can be accessed with
|
1
|
nDimensionalArray[index1, ..., indexN]
|
Another concept you should be aware of is the length of multidimensional arrays. Of course, we can't access a Length property directly, like we used for a one-dimensional array, that is not possible. That is because each dimension has its own length, which can be retrieved this way:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
using
System;
namespace
HelloWorld
{
class
Program
{
static void Main(string[] args)
{
int[,] matrix =
{
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
};
matrix.GetLength(0);
matrix.GetLength(1);
Console.ReadLine();
}
}
}
|
At a first sight, you’d think that matrix.GetLength(0) would return the length of the first row, while matrix.GetLength(1) the values of the second, and that would be a nasty trap! Actually, matrix.GetLength(0) returns the length of our first dimension – the rows – which in our case is 2, while matrix.GetLength(1) returns the length of our second dimension – the columns – which is 4!
To conclude this lesson, this is how we display the elements of a matrix to the console:
|
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
|
using
System;
namespace
HelloWorld
{
class
Program
{
static void Main(string[] args)
{
int[,] matrix =
{
{ 1, 2, 3, 4 }, // row 0 values
{ 5, 6, 7, 8 }, // row 1 values
};
// print the matrix on the console
for (int row = 0; row < matrix.GetLength(0); row++)
{
for (int col = 0; col < matrix.GetLength(1); col++)
{
Console.Write(matrix[row, col]);
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
|
First we declare and initialize an array. The array is two-dimensional, therefore we use a for loop which will iterate through the rows, and a nested for loop which for each row will iterate through the columns. At each iteration we will print the current element using the appropriate method to access this element by using its two indices (row and column). Finally, if we execute this piece of code we will get the following result:
The concepts explained in this lesson are also shown visually as part of the following video: