🧱 Arrays · Beginner

Multi-dimensional & jagged arrays in Java

int[][] as array of arrays with different row lengths.

🧩 The mysteryPascal's triangle has 1 number in row one, 2 in row two, 3 in row three... Can a Java grid have rows of different lengths? Yes, because it isn't really a grid.

Arrays inside an array

Java has no true 2-D arrays. An int[][] is an **array whose elements are int[] rows**. new int[2][3] makes 2 rows of 3. Use grid[r][c]: first pick the row, then the column. **grid.length is the number of rows**; grid[r].length is the length of row r.

int[][] grid = new int[2][3]; // 2 rows x 3
grid[1][2] = 5;
grid.length     // 2 (rows)
grid[0].length  // 3 (row 0's length)
🔮 Predict it

Your turn

What prints?

int[][] g = new int[4][2];
g[3][1] = 7;
String out = g.length + " " + g[0].length;
System.out.println(out + " " + g[3][1]);
  1. 4 2 7
  2. 2 4 7
  3. 8 2 7
  4. 4 2 0
Show the answer

The first size is the number of rows (4); each row is an int[2]. Row 3, column 1 was set to 7.

Jagged arrays

Each row is its own int[] object, so rows can have different lengths: a jagged array. Always ask each row for its own length.

int[][] tri = {
    {1},
    {1, 1},
    {1, 2, 1}
};
int x = tri[2][1];     // 2
int n = tri[1].length; // 2
🔮 Predict it

Your turn

What prints?

int[][] t = {{5}, {6, 7}, {8, 9, 10}};
int total = 0;
for (int[] row : t) {
    total += row[0];
}
System.out.println(total + " " + t[2].length);
  1. 19 3
  2. 24 3
  3. 19 6
  4. 5 3
Show the answer

for-each hands you one row at a time. Their first elements are 5, 6 and 8: total 19. The last row has 3 elements.

⚠️ The trap

Rows that don't exist yet

new int[2][] creates only the outer array. Its rows start as null until you create them. Touching m[1][0] before m[1] exists throws NullPointerException.

int[][] m = new int[2][];
m[0] = new int[]{1, 2};
m[0][1]; // 2
m[1][0]; // NullPointerException

Looping over a jagged array

✗ Assumes equal rows
for (int r = 0; r < m.length; r++)
    for (int c = 0; c < m[0].length; c++)
        System.out.print(m[r][c]);

Every row is assumed as long as row 0. A shorter row throws ArrayIndexOutOfBoundsException.

✓ Asks each row
for (int r = 0; r < m.length; r++)
    for (int c = 0; c < m[r].length; c++)
        System.out.print(m[r][c]);

m[r].length is safe for any shape.

💼 In the real world

In real projects

Game boards, seating plans, spreadsheets and images (pixels[y][x]) are 2-D arrays. Jagged arrays save memory for triangular data like distance tables, or rows of varying size like the number of seats per row in a theater.

Key takeaways

  1. int[][] is an array of int[] rows
  2. grid.length = rows; grid[r].length = length of row r
  3. Rows may have different lengths (jagged)
  4. new int[3][] creates only the outer array; its rows start as null
🤯 Did you know?

int[] a[] is legal Java: it mixes both bracket styles and declares a 2-D array. Legal, yes. Readable, no. Please don't.

Practice questions

What does this print?

int[][] g = new int[3][4];
System.out.println(g.length + " " + g[0].length);
  1. 4 3
  2. 3 4
  3. 12 4
  4. 3 3
Check your answer

3 4. The first size is the number of rows (3); each row is an int[4].

What does this print?

int[][] t = {{1}, {2, 3}, {4, 5, 6}};
int sum = 0;
for (int[] row : t) {
    sum += row[row.length - 1];
}
System.out.println(sum);
  1. 21
  2. 6
  3. 10
  4. Throws ArrayIndexOutOfBoundsException
Check your answer

10. Each row's last element is used: 1 + 3 + 6 = 10. Using row.length (not a fixed number) keeps the loop safe for jagged rows.

Next: stop writing loops to sort, fill and print. Java ships a whole toolbox for arrays.