Java Multidimensional Arrays
A Java multidimensional array is an array whose elements are also arrays. The most common form is a two-dimensional array, which you can picture as rows and columns.
Multidimensional arrays are useful for grid-like data such as tables, seats, game boards, calendars, and scores grouped by category.
Create A Two-Dimensional Array
A two-dimensional array uses two pairs of square brackets. The first index chooses the row, and the second index chooses the column.
public class Main {
public static void main(String[] args) {
int[][] scores = {
{85, 90, 78},
{92, 88, 95}
};
System.out.println("First row, first column: " + scores[0][0]);
System.out.println("Second row, third column: " + scores[1][2]);
scores[0][2] = 80;
System.out.println("Updated value: " + scores[0][2]);
}
}
Output:
First row, first column: 85
Second row, third column: 95
Updated value: 80
The value scores[0][0] means row 0, column 0. Like regular arrays, both indexes start at 0. The statement scores[0][2] = 80; changes the third value in the first row.
Rows And Columns
Use array.length to get the number of rows. Use array[row].length to get the number of columns in a specific row.
public class Main {
public static void main(String[] args) {
String[][] seats = {
{"A1", "A2", "A3"},
{"B1", "B2", "B3"},
{"C1", "C2", "C3"}
};
System.out.println("Rows: " + seats.length);
System.out.println("Columns in row 0: " + seats[0].length);
}
}
Output:
Rows: 3
Columns in row 0: 3
In this example, seats has three rows. Each row is a String[] with three elements.
Loop Through A Two-Dimensional Array
To visit every value, use a nested loop. The outer loop moves through rows, and the inner loop moves through columns inside the current row.
public class Main {
public static void main(String[] args) {
int[][] grid = {
{1, 2, 3},
{4, 5, 6}
};
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[row].length; col++) {
System.out.print(grid[row][col] + " ");
}
System.out.println();
}
}
}
Output:
1 2 3
4 5 6
The condition col < grid[row].length is important because it checks the length of the current row. This works for regular grids and for rows with different lengths.
Jagged Arrays
Java multidimensional arrays are really arrays of arrays, so each row can have a different length. This is called a jagged array.
public class Main {
public static void main(String[] args) {
int[][] weeklySales = {
{12, 9},
{8, 11, 14},
{10}
};
for (int day = 0; day < weeklySales.length; day++) {
int total = 0;
for (int sale : weeklySales[day]) {
total += sale;
}
System.out.println("Day " + (day + 1) + " total: " + total);
}
}
}
Output:
Day 1 total: 21
Day 2 total: 33
Day 3 total: 10
The first row has two values, the second row has three values, and the third row has one value. Because the loop reads weeklySales[day], each row can be processed safely.
Create An Empty Multidimensional Array
You can create a rectangular array by giving the number of rows and columns. Java fills numeric values with 0 until you assign something else.
public class Main {
public static void main(String[] args) {
int[][] matrix = new int[2][3];
matrix[0][0] = 7;
matrix[1][2] = 9;
System.out.println(matrix[0][0]);
System.out.println(matrix[0][1]);
System.out.println(matrix[1][2]);
}
}
Output:
7
0
9
The array has two rows and three columns. The value matrix[0][1] was never assigned, so it keeps the default value 0.
Common Mistakes
- Using
array.lengthas the column count instead ofarray[row].length. - Forgetting that both row and column indexes start at
0. - Trying to access a column that does not exist in a jagged row.
- Reading
array[0].lengthbefore making sure the array has at least one row.
Takeaway: a Java multidimensional array stores arrays inside an array, and nested loops let you work through its rows and columns safely.
