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 |
int [,] twoDimensionalArray; |

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 |
int [,,] threeDimensionalArray; |

and so on. However, two dimensional arrays are rarely used, and more than two dimensions ever more rarely. However, in theory, you could declare an infinite number of dimensions.

We declare multidimensional array like so:

1 2 3 |
int[,] intMatrix; float[,] floatMatrix; string[,,] strCube; |

and we can initialize them like this:

1 2 3 |
int[,] intMatrix = new int[3, 4]; float[,] floatMatrix = new float[8, 2]; string[,,] stringCube = new string[5, 5, 5]; |

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

*based indexed.*

**0**Lastly, this is how we initialize our multidimensional arrays with some values:

1 2 3 4 5 6 |
int[,] matrix = { {1, 2, 3, 4}, // row 0 values, with 4 cols {5, 6, 7, 8}, // row 1 values, with 4 cols }; // The matrix size is 2 x 4 (2 rows, 4 columns) |

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 following declaration:

1 2 3 4 5 |
int[,] _matrix = { {1, 2, 3, 4}, {5, 6, 7, 8}, }; |

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 cant 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 |
int[,] _matrix = { {1, 2, 3, 4}, {5, 6, 7, 8}, }; _matrix.GetLength(0); _matrix.GetLength(1); |

At a first sight, you’d think that * _matrix.GetLength(0)* would return the length of the first row, while

*the values of the second, and that would be a nasty trap! Actually,*

**_matrix.GetLength(1);***returns the length of our first dimension – the rows – which in our case is 2, while*

**_matrix.GetLength(0)***returns the length of our second dimension – the columns – which is 4!*

**_matrix.GetLength(1)**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 |
// Declare and initialize a matrix of size 2 x 4 int[,] _matrix = { {1, 2, 3, 4}, // row 0 values {5, 6, 7, 8}, // row 1 value }; // 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(); } |

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:

### Comments

Tags: arrays, multidimensional arrays