Arrays with More Than One Dimension
A one-dimensional array, also known as a vector in mathematics, stores data in a linear sequence. However, when we need to represent more complex data structures like a chess board or a table with rows and columns, we use multi-dimensional arrays. A two-dimensional array is often called a matrix in mathematics.
Arrays can also have more than two dimensions, but these can become complicated to manage and visualize. For now, we'll focus on one-dimensional and two-dimensional arrays.
Declaring Multi-Dimensional Arrays
Multi-dimensional arrays in C# are declared similarly to one-dimensional arrays, but with additional commas to represent each dimension:
As with one-dimensional arrays, memory needs to be allocated using the new
keyword:
In myTable
, the first dimension represents the number of rows, and the second dimension represents the number of columns. This means myTable
has 4 rows and 5 columns:
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 3 | 7 | 4 | 2 | 1 |
1 | 7 | 2 | 8 | 9 | 8 |
2 | 5 | 5 | 2 | 0 | 5 |
3 | 3 | 9 | 5 | 2 | 6 |
Initializing a 2D Array
Just like one-dimensional arrays, a two-dimensional array can be initialized with values using curly braces:
This array has 2 rows and 4 columns.
Accessing Elements in a 2D Array
To access an element in a two-dimensional array, you need to specify both indices: the row index and the column index.
Each dimension of the array has its own length, which can be accessed using the GetLength()
method:
matrix.GetLength(0);
returns 2 (number of rows).matrix.GetLength(1);
returns 4 (number of columns).
Iterating Through a 2D Array
To iterate through all elements in a two-dimensional array, we typically use two nested loops, one for each dimension:
This will print every element in the array, along with its position.
Practical Example: Representing a Chessboard
A chessboard can be represented as an 8x8 two-dimensional array, where each element could store a piece's information or whether the square is empty:
Handling More Dimensions
For three-dimensional arrays, the concept extends with an additional index. For example, a cube
with dimensions [3, 3, 3]
could represent a 3x3x3 Rubik's Cube. Each element in the cube would then require three indices to be accessed.
Summary
Multi-dimensional arrays extend the concept of simple arrays to allow for more complex data representations, such as tables, grids, and even 3D structures. Understanding how to use them is crucial for handling structured data efficiently in programming.