Lesson · Arrays
Multidimensional arrays: arrays of arrays
10 min readMultidimensional arrays
Java has no true 2D arrays. What it has is arrays whose elements are arrays, which is why rows can differ in length and why each row is a separate allocation.
An array of references to arrays
int[][] grid = new int[3][4]; is a one-dimensional array of three elements, each a reference to its own int[4]. Two allocations happen (the outer array and one inner per row); the outer array's elements are references, the inner ones hold the numbers. grid.length is 3, grid[0].length is 4, and grid[row][col] is sugar for grid[row] followed by [col].
The model predicts everything unusual. new int[3][] creates three null rows, to be filled later. grid[1][7] compiles because the static type is int[], and reads the second row's length at runtime. A ragged grid where each row has its own length is not an oddity: it is the natural consequence of the model.
Creating and filling grids
The rectangular new int[3][4] allocates every row in one statement. The staged form allocates rows individually, which is what ragged structures (a triangle, a jagged matrix) require: int[][] tri = new int[3][]; tri[0] = new int[1]; tri[1] = new int[2]; and so on for a lower triangle.
Literals nest naturally: int[][] m = { { 1, 2 }, { 3, 4, 5 } }; is a ragged grid built in one expression. Access remains uniform: m[i][j] for valid i and j.
Traversal: row, then column
Nested for-each is the scanning idiom: for (int[] row : grid) for (int cell : row) .... It never touches an index, so it cannot fall off any bound, and it works unchanged on ragged and rectangular grids. The indexed double loop takes over when positions matter: matrix multiplication, transposes, diagonals, scanning neighbours.
In the indexed form the boundaries differ per loop level: row < grid.length, then col < grid[row].length. Writing grid[0].length in the inner test is correct only for a rectangular grid and goes quietly wrong on a ragged one. The per-row reference is the honest one.
The classic pitfalls
Arrays.toString(grid) prints the outer array's references, not the contents: what you want is Arrays.deepToString(grid). A shallow clone (the default clone()) copies the outer array only, so both clones share every row: a write through either appears in both. Deep copies are per-row clones in a loop, and the arrays-internals topic explains why nothing automatic is offered.
Equality has the same shape: Arrays.equals is shallow and returns false for equal-content grids, while Arrays.deepEquals compares recursively and returns true. The deep/shallow distinction is a recurring character in Java's API design, and two-dimensional structures are where it first bites.
Delete the phrase true 2D array from your vocabulary
There is no rectangular memory block behind an int[][]; there is an outer array of row references. Every difference between Java grids and, say, C's contiguous 2D arrays (ragged rows, per-row allocation, shallow clones doing nothing useful) follows from that one fact.
Interview angles
How many objects does new int[3][4] create?+
Four: one outer int[3][] and three int[4] rows. The outer array stores references, which is also why ragged rows are allowed and why clone() follows only the outer level.
Why does Arrays.toString fail on a grid?+
It formats elements with their toString, and each outer element is an int[] whose default toString is a class name and hash. Arrays.deepToString recurses into nested arrays; deepEquals is its comparison twin.
Safe way to traverse a ragged grid?+
Nested for-each binds each row reference first, so lengths are never assumed: for (int[] row : grid) for (int cell : row). In index form, test each level against its own array: grid[row].length inside, never grid[0].length.