A multi-dimensional array is an extension of a regular array that allows storing data in multiple dimensions—such as rows and columns, or more.
It is essentially an array of arrays. For example, a two-dimensional array can be thought of as a table with rows and columns.
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
# Access element in 2nd row, 3rd column (indexing starts at 0)
print(matrix[1][2]) # Output: 6
// Declare and initialize 2D array
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Access element at row 1, column 2
System.out.println(matrix[1][2]); // Output: 6
Multidimensional arrays are powerful tools for organizing complex data structures in efficient ways and form a critical foundation for many algorithms.