Multidimensional Arrays in Java
A multidimensional array is an array of arrays. int[][] grid declares a 2D array where each element is itself an int[], letting data be organized into rows and columns. grid[row][col] accesses one specific element by indexing twice — once for the row, once for the column within that row.
Because each row is genuinely its own separate array, rows don't have to be the same length. A jagged array, where grid[0] has 3 elements and grid[1] has 5, is entirely legal in Java, unlike languages that enforce a strict rectangular grid.
Declaring a 2D array
int[][] grid = new int[3][4]; creates 3 rows, each an array of 4 ints.
Accessing an element
grid[row][col] indexes into the outer array first, then into that row's own array.
It's really an array of arrays
grid[0] alone refers to the first row, which is itself a complete, independent int[].
Jagged arrays are legal
Since each row is its own array, rows can have different lengths from each other.
Iterating a 2D array
A nested loop, one level per dimension, is the standard way to visit every element.
public class GridDemo { public static void main(String[] args) { int[][] grid = { {1, 2, 3}, {4, 5, 6} }; System.out.println("Element at [1][2]: " + grid[1][2]); System.out.println("Row count: " + grid.length); }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.