In the previous topic, we learned about arrays, where we can store multiple values in a single variable. But what if our data needs to be organized into rows and columns?
For example, imagine a classroom with 3 rows of desks, where each row has 4 desks. Or imagine a spreadsheet containing rows and columns of data. A normal one-dimensional array isn't the best way to represent this kind of structure.
This is where a 2D array comes in.
A 2D array is basically an array organized into rows and columns. You can think of it like a table.
Column
0 1 2
┌────┬────┬────┐
Row 0 │ 10 │ 20 │ 30 │
├────┼────┼────┤
Row 1 │ 40 │ 50 │ 60 │
├────┼────┼────┤
Row 2 │ 70 │ 80 │ 90 │
└────┴────┴────┘
Creating a 2D Array
We can create a 2D array like this:
int[][] numbers = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};
Here, we have 3 rows, and each row contains 3 values.
The first pair of square brackets represents the rows, while the second represents the columns.
Accessing Elements
Just like a normal array, a 2D array uses indexes. The difference is that we need two indexes: one for the row and one for the column.
For example:
System.out.println(numbers[0][1]);
The first 0 means row 0, and the second 1 means column 1.
Looking at our array:
10 20 30
40 50 60
70 80 90
numbers[0][1] gives us:
20
Similarly:
System.out.println(numbers[2][0]);
gives:
70
Remember that both rows and columns start from index 0.
Changing a Value
We can also change an element using its row and column indexes.
int[][] numbers = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};
numbers[1][2] = 100;
The array now becomes:
10 20 30
40 50 100
70 80 90
We changed the value at row 1, column 2.
Using Nested Loops
This is where the nested loops we learned earlier become very useful.
We can use one loop for the rows and another loop for the columns.
int[][] numbers = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};
for (int row = 0; row < numbers.length; row++) {
for (int column = 0; column < numbers[row].length; column++) {
System.out.print(numbers[row][column] + " ");
}
System.out.println();
}
Output:
10 20 30
40 50 60
70 80 90
The outer loop moves through the rows, while the inner loop moves through the columns of each row.
This is one of the most common ways to work with 2D arrays.
Finding the Number of Rows and Columns
We can use the length property to find the number of rows:
numbers.length
For the number of columns in a particular row, we can use:
numbers[row].length
For example:
int[][] numbers = {
{10, 20, 30},
{40, 50, 60}
};
System.out.println(numbers.length);
System.out.println(numbers[0].length);
Output:
2
3
There are 2 rows, and the first row contains 3 elements.
A Real-World Example
Imagine a teacher wants to store the marks of three students in three subjects. A 2D array can represent this data nicely.
int[][] marks = {
{85, 90, 78},
{72, 88, 91},
{95, 82, 89}
};
You can think of it like this:
Math English Science
John 85 90 78
Jason 72 88 91
Alex 95 82 89
If we wanted John's Science mark, we could access:
marks[0][2]
which gives us 78.
Creating an Empty 2D Array
We can also create a 2D array by specifying its size:
int[][] numbers = new int[3][4];
This creates an array with 3 rows and 4 columns.
We can then assign values:
numbers[0][0] = 10;
numbers[0][1] = 20;
numbers[1][0] = 30;
The remaining integer elements will initially contain their default value, which is 0.
Important Point
A 2D array in Java is technically an array of arrays. This means different rows can even have different lengths.
For example:
int[][] numbers = {
{10, 20},
{30, 40, 50},
{60}
};
This is valid Java.
The first row has 2 elements, the second has 3, and the third has 1.
This type of structure is sometimes called a jagged array.
For now, the main thing to remember is that a 2D array allows us to organize data using rows and columns, and nested loops make it easy to process that data.