Multidimensional Arrays
In short: An array whose elements are themselves arrays — in practice mostly two-dimensional, used e.g. for tables or grid data.
In more detail: Declaration e.g. int[][] matrix = new int[3][4]; for a 3×4 grid. Java implements this internally as an “array of arrays”, where rows don’t necessarily have to be the same length (jagged arrays). Access via two indices: matrix[row][column].
In Depth
int[][] matrix = new int[3][4]; // 3 rows, 4 columns, all values auto-initialized to 0
matrix[0][0] = 1;
matrix[2][3] = 99;
// Initialize directly with values
int[][] tictactoe = {
{1, 0, 1},
{0, 1, 0},
{1, 0, 0}
};
// Iterating over a 2D structure - nested loops
for (int row = 0; row < tictactoe.length; row++) {
for (int column = 0; column < tictactoe[row].length; column++) {
System.out.print(tictactoe[row][column] + " ");
}
System.out.println();
}
// Jagged array: rows with DIFFERENT lengths
int[][] jagged = new int[3][];
jagged[0] = new int[]{1};
jagged[1] = new int[]{1, 2, 3};
jagged[2] = new int[]{1, 2};Because Java actually implements multidimensional arrays internally as an “array whose elements are themselves arrays” (not as a single contiguous block of memory as in some other languages), each row doesn’t necessarily have to be the same length — such “jagged” arrays are completely valid in Java, even if their practical use is rather rare. When iterating over 2D structures, tictactoe[row].length (the length of the INDIVIDUAL row) matters instead of the outer .length, as soon as jagged arrays are involved — otherwise ArrayIndexOutOfBoundsExceptions loom for shorter rows.
Memory layout: array of arrays
Unlike, say, in C, where a 2D array exists as a single, contiguous block of memory, a Java 2D array is technically an array whose elements are each REFERENCES to independent, separate 1D arrays — every row is its own object on the heap, scattered rather than guaranteed to be contiguous. This explains both the possibility of jagged arrays (every row is independent and can have a different length) and why matrix.length only returns the number of ROWS, not the total number of all elements.
Three-dimensional and higher-dimensional arrays
The principle can be extended indefinitely — int[][][] cube = new int[3][4][5]; creates a three-dimensional array, accessible via three indices (cube[x][y][z]). In practice, more than two dimensions are rarely needed (most likely for scientific calculations or volumetric data) — for most everyday tasks with structured data, collections like nested Lists or custom classes are often the more readable alternative to deeply nested arrays.
See also: Arrays, Index, Collections