Multi-dimensional & jagged arrays in Java
int[][] as array of arrays with different row lengths.
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)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]);4 2 72 4 78 2 74 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; // 2Your 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);19 324 319 65 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.
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]; // NullPointerExceptionLooping over a jagged array
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.
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 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
- int[][] is an array of int[] rows
- grid.length = rows; grid[r].length = length of row r
- Rows may have different lengths (jagged)
- new int[3][] creates only the outer array; its rows start as null
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);- 4 3
- 3 4
- 12 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);- 21
- 6
- 10
- 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.