Java Multidimensional Arrays
Work with two-dimensional and higher-dimensional arrays in Java, including jagged arrays.
Java doesn't actually have a separate "2D array" type. What it has is arrays of arrays — an int[][] is an array whose elements are themselves int[]s. The syntax is short enough that it looks like a built-in grid, and most of the time you can use it that way.
Declaring a 2D array
Stack another pair of brackets on the type:
int[][] grid;
String[][] board;Allocate with two sizes — rows first, then columns:
int[][] grid = new int[3][4]; // 3 rows, 4 columns, all zeroOr use a nested literal:
int[][] grid = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};The outer braces hold three sub-arrays, each of length 4.
Accessing elements
Two indexes: row first, then column.
int[][] grid = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
System.out.println(grid[0][0]); // 1
System.out.println(grid[2][1]); // 8
grid[1][1] = 50; // replace 5 with 50grid[0] on its own is the first row — itself an int[] of length 3. That's the key thing to internalize: indexing once gives you a row, indexing twice gives you a cell.
length for rows and columns
grid.length is the number of rows. grid[r].length is the number of columns in row r:
int rows = grid.length;
int cols = grid[0].length; // assuming row 0 existsThere's no top-level grid.cols because Java doesn't assume every row has the same length — see jagged arrays below.
Iterating
The standard double-for:
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[r].length; c++) {
System.out.print(grid[r][c] + " ");
}
System.out.println();
}Or with enhanced for, treating each row as an int[]:
for (int[] row : grid) {
for (int cell : row) {
System.out.print(cell + " ");
}
System.out.println();
}Use the enhanced form when you don't care about coordinates, the classic form when you do.
Jagged arrays
The inner arrays don't have to be the same length. An array of arrays in Java can be jagged — each row sized independently:
int[][] triangle = new int[4][]; // 4 rows, columns unspecified
triangle[0] = new int[]{1};
triangle[1] = new int[]{1, 2};
triangle[2] = new int[]{1, 2, 3};
triangle[3] = new int[]{1, 2, 3, 4};Or with a literal:
int[][] triangle = {
{1},
{1, 2},
{1, 2, 3},
{1, 2, 3, 4}
};Iterating works exactly the same — you ask each row for its own .length:
for (int r = 0; r < triangle.length; r++) {
for (int c = 0; c < triangle[r].length; c++) {
System.out.print(triangle[r][c] + " ");
}
System.out.println();
}This is why int[][] is "array of arrays" rather than a true matrix: nothing in the language forces every row to be the same width.
Higher dimensions
The pattern extends. int[][][] is an array of arrays of arrays — useful for a 3D grid, a stack of matrices, an RGB image (height × width × 3):
int[][][] cube = new int[2][3][4]; // 2 × 3 × 4
cube[0][1][2] = 99;In practice anything beyond 2D is rare in idiomatic Java — at that point a class with named fields is almost always clearer.
A worked example
What's next
You've now seen arrays in one and more dimensions. Next we'll look at the everyday array operations — getting the length, filling, finding, copying — and the standard library helpers that make most of them one-liners.
Practice
What is grid[0] for an int[][] grid?