Methods, Arrays & Strings

Multidimensional Arrays

15 min Lesson 7 of 14

Multidimensional Arrays

So far every array you have worked with holds a flat list of values. Real problems often need more structure: a grid of pixels, a timetable, or a chessboard all demand rows and columns. Java handles this with multidimensional arrays — arrays whose elements are themselves arrays.

Declaring and Creating a 2D Array

The most common case is a two-dimensional (2D) array, often pictured as a table with rows and columns. The declaration adds one extra pair of brackets:

// declare and allocate a 3-row, 4-column grid of ints int[][] grid = new int[3][4];

Java allocates three inner arrays, each of length four. Every cell starts at its default value (0 for int). You can also initialise it inline with an array literal:

int[][] scores = { {85, 90, 78}, // row 0 — student A {92, 88, 95}, // row 1 — student B {70, 75, 80} // row 2 — student C };

Each inner array is a row. Think of scores[1][2] as "row 1, column 2" — the value 95.

Accessing Elements

You index a 2D array with two bracket expressions: the first for the row, the second for the column.

System.out.println(scores[0][1]); // 90 (row 0, column 1) scores[2][0] = 73; // update row 2, column 0
Row-major order. In Java a 2D array is actually an array of arrays. scores[1] is itself an int[] — you can pass it anywhere a 1D array is expected. This matters when you need to process one row at a time.

Iterating Over a 2D Array

Nested for loops are the standard way to visit every cell. The outer loop walks the rows; the inner loop walks the columns.

int rows = scores.length; // number of rows int cols = scores[0].length; // number of columns in row 0 for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { System.out.print(scores[r][c] + "\t"); } System.out.println(); // newline after each row }

Output:

85 90 78 92 88 95 73 75 80

The enhanced for-each loop also works, treating each element of the outer array as a row:

for (int[] row : scores) { for (int value : row) { System.out.print(value + "\t"); } System.out.println(); }

A Practical Grid Example

Let us build a simple multiplication table — a classic 2D grid problem:

int size = 5; int[][] table = new int[size][size]; for (int r = 0; r < size; r++) { for (int c = 0; c < size; c++) { table[r][c] = (r + 1) * (c + 1); } } // print it for (int[] row : table) { for (int cell : row) { System.out.printf("%4d", cell); } System.out.println(); }

Output:

1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25
Use length — never hard-code dimensions. Write grid.length for rows and grid[0].length for columns. If the array is later resized, your loops still work without any changes.

Jagged Arrays

Java does not require every row to have the same length. When you allocate only the outer array and fill the rows manually, you get a jagged array (also called a ragged array).

int[][] triangle = new int[4][]; // outer array: 4 rows, no inner arrays yet for (int r = 0; r < triangle.length; r++) { triangle[r] = new int[r + 1]; // row 0 has 1 element, row 1 has 2, etc. for (int c = 0; c <= r; c++) { triangle[r][c] = c + 1; } } // print it for (int[] row : triangle) { for (int val : row) { System.out.print(val + " "); } System.out.println(); }

Output:

1 1 2 1 2 3 1 2 3 4
Watch out for NullPointerException. When you allocate only the outer dimension (new int[4][]), each row is null until you assign it. Accessing triangle[1][0] before assigning triangle[1] throws a NullPointerException.

Arrays with More Than Two Dimensions

Java allows three or more dimensions — just keep adding bracket pairs. A 3D array can represent a cube of values (for example, pixel data with width, height, and colour channels).

// A 2x3x4 three-dimensional array int[][][] cube = new int[2][3][4]; cube[1][2][3] = 42; System.out.println(cube[1][2][3]); // 42

In practice, 2D arrays cover the vast majority of use cases. Go beyond two dimensions only when the data genuinely has that structure — the mental model and the code both get harder quickly.

Summary

  • A 2D array is declared with int[][] name = new int[rows][cols] and indexed with name[row][col].
  • Use nested loops to iterate; the outer loop indexes rows and the inner loop indexes columns.
  • Always use array.length and array[0].length instead of hard-coded numbers.
  • Jagged arrays allow rows of different lengths — allocate the outer array first, then each row separately.
  • Avoid accessing a row before it has been assigned or you will get a NullPointerException.