Learn Java
import com.learnjava.datastructurescollections.MultidimensionalArrays;

Multidimensional Arrays in Java

@Concept

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.

@HowItWorks
1

Declaring a 2D array

int[][] grid = new int[3][4]; creates 3 rows, each an array of 4 ints.

2

Accessing an element

grid[row][col] indexes into the outer array first, then into that row's own array.

3

It's really an array of arrays

grid[0] alone refers to the first row, which is itself a complete, independent int[].

4

Jagged arrays are legal

Since each row is its own array, rows can have different lengths from each other.

5

Iterating a 2D array

A nested loop, one level per dimension, is the standard way to visit every element.

@Example
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);
}
}
Output
Element at [1][2]: 6
Row count: 2
1grid is built as an array of two rows, each itself a 3-element int array.
2grid[1][2] first selects the second row ({4, 5, 6}), then the third element of that row, which is 6.
3grid.length gives the number of rows, since grid itself is just an array whose elements happen to be other arrays.
4Both println calls run once each, giving two lines of output.
@UseCases
Representing a grid-based game board, spreadsheet, or image's pixel data
Storing a matrix for mathematical operations like addition or multiplication
Building a jagged array where each row legitimately needs a different length
Visiting every cell of a 2D structure using a nested loop

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.