Two-dimensional arrays

Java has no two-dimensional array in the language. It has an array whose elements are arrays, and int[][] is a type whose values are references to such arrays. Every row is an object of its own, and that is what makes a grid, a triangle and a ragged table the same type.

A grid

new int[3][3] creates three rows and puts a three-element array in each. The first index chooses the row and the second chooses the element inside that row.

The file Grid.java:

public class Grid { public static void main(String[] args) { int[][] grid = new int[3][3]; for (int row = 0; row < grid.length; row++) { for (int col = 0; col < grid[row].length; col++) { grid[row][col] = row * 3 + col; } } for (int[] row : grid) { for (int value : row) { System.out.print(value + " "); } System.out.println(); } } }

javac -Xlint:all Grid.java
java Grid

0 1 2
3 4 5
6 7 8

The two loops are the same loops as in a one-dimensional array, one inside the other. The outer enhanced loop gives a whole row, and the inner one walks that row; the indexed form is needed only when the column number matters to the body.

Two lengths

grid.length is the number of rows. grid[0].length is the length of the first row, and every row has its own. A loop that writes grid[0].length for every row is correct only for a rectangular grid, which is why the inner loop reads grid[row].length.

A row is a reference, so grid[0] can be replaced by another array, and two rows can even be the same array. That is legal and usually a mistake: writing to one row then changes the other.

Ragged arrays

new int[3][] creates three row slots, each null, and the program fills each with an array of its own length. The result is a triangle, and reading the length of a row that was never filled throws NullPointerException.

public class Ragged { public static void main(String[] args) { int[][] rows = new int[3][]; rows[0] = new int[] {1}; rows[1] = new int[] {2, 3}; rows[2] = new int[] {4, 5, 6}; for (int i = 0; i < rows.length; i++) { System.out.println("row " + i + ": " + rows[i].length); } } }

javac -Xlint:all Ragged.java
java Ragged

row 0: 1
row 1: 2
row 2: 3

The lengths are 1, 2 and 3 because that is how the three rows were built. A rectangular grid and a ragged one are the same type, and a method that takes int[][] cannot assume which it received.

A transposed matrix

A transpose swaps rows and columns: the element at [row][col] moves to [col][row]. The result of transposing a 2 by 3 grid is a 3 by 2 grid, so the new array is created with the lengths of the old one swapped.

public class Transpose { public static void main(String[] args) { int[][] grid = {{1, 2, 3}, {4, 5, 6}}; int[][] transposed = new int[grid[0].length][grid.length]; for (int row = 0; row < grid.length; row++) { for (int col = 0; col < grid[row].length; col++) { transposed[col][row] = grid[row][col]; } } for (int[] row : transposed) { System.out.println(java.util.Arrays.toString(row)); } } }

javac -Xlint:all Transpose.java
java Transpose

[1, 4]
[2, 5]
[3, 6]

The first row of the result is the first column of the input, 1 and 4. The assignment is the whole algorithm: one line, two indices swapped. A square matrix can be transposed in place by swapping the elements above the diagonal with the ones below it.

Exercises

  1. Print the sum of every row of the grid in Grid.java on its own line.
  2. Build a ragged array of four rows with lengths 4, 3, 2 and 1 and print its lengths.
  3. Transpose a square 3 by 3 matrix in place by swapping [i][j] with [j][i] for j > i.
  4. Read rows[1].length before assigning the row in Ragged.java. What does the JVM throw?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is an int[][] in Java?

    Answer

    An array whose elements are int arrays

    The type is an array of references to int arrays, which is why rows may have different lengths.

  2. What is grid.length for a grid of three rows?

    Answer

    3, the number of rows

    The outer array has one element per row, so its length is the number of rows.

  3. Why does the inner loop read grid[row].length rather than grid[0].length?

    Answer

    Rows may have different lengths

    A ragged array has a length per row, so the current row supplies it.

  4. What does this program print? public class Q1 { public static void main(String[] args) { int[][] grid = {{1, 2}, {3, 4}}; System.out.println(grid.length + " " + grid[0].length); } }

    Answer

    2 2

    There are two rows and the first row has two elements.

  5. What does this program print? public class Q2 { public static void main(String[] args) { int[][] grid = {{1, 2, 3}, {4, 5, 6}}; int sum = 0; for (int[] row : grid) { for (int value : row) { sum += value; } } System.out.println(sum); } }

    Answer

    21

    The six values add up to 21.

  6. What does this program print? public class Q3 { public static void main(String[] args) { int[][] rows = new int[2][]; rows[0] = new int[] {1}; rows[1] = new int[] {2, 3, 4}; System.out.println(rows[0].length + " " + rows[1].length); } }

    Answer

    1 3

    The first row has one element and the second has three.

  7. What does this program print? public class Q4 { public static void main(String[] args) { int[][] grid = {{1, 2}, {3, 4}}; int[][] copy = new int[2][2]; for (int row = 0; row < 2; row++) { for (int col = 0; col < 2; col++) { copy[col][row] = grid[row][col]; } } System.out.println(copy[0][1]); } }

    Answer

    3

    The transpose puts grid[1][0], which is 3, at copy[0][1].

  8. What happens when a row of new int[3][] is read before it is assigned?

    Answer

    NullPointerException

    Each row slot starts as null, so reading its length dereferences null.

  9. How many arrays exist for new int[3][4]?

    Answer

    Four: the outer array and three rows

    The outer array holds three references, and new creates one four-element array for each row.

  10. What does this program print? public class Q5 { public static void main(String[] args) { int[][] grid = new int[2][3]; System.out.println(grid[1][2]); } }

    Answer

    0

    Every element of a new int grid is zero, including the last one.

The code on this page was built with OpenJDK 21.0.13 (javac and java) and run on Alpine Linux in a Docker container.

Article author: Arthur Isaev