A multidimensional array is an array whose elements are themselves arrays. In Java, a two-dimensional array is commonly used to store data in rows and columns, like a table.
A one-dimensional array stores values in a single sequence. A two-dimensional array organizes values into rows and columns.
One-dimensional array:
int[] numbers = {10, 20, 30, 40};
Two-dimensional array:
int[][] numbers = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};
This array contains three rows and three columns.
The common declaration syntax is:
dataType[][] arrayName;
Examples:
int[][] numbers;
String[][] names;
double[][] prices;
These declarations specify the type of each element. The array must be created before you can access its elements.
int[][] numbers = new int[3][4];
This creates an array with three rows and four columns. All integer elements initially contain 0.
You can initialize a two-dimensional array with values.
int[][] numbers = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Each inner array represents one row.
| Row / Column | Column 0 | Column 1 | Column 2 |
|---|---|---|---|
| Row 0 | 1 | 2 | 3 |
| Row 1 | 4 | 5 | 6 |
| Row 2 | 7 | 8 | 9 |
To access an element, use two indexes: the row index and the column index.
int[][] numbers = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};
System.out.println(numbers[0][0]);
System.out.println(numbers[1][2]);
System.out.println(numbers[2][1]);
Output:
10
60
80
Remember that both row and column indexes start at 0.
You can update an element by assigning a new value to its indexes.
int[][] numbers = {
{10, 20, 30},
{40, 50, 60}
};
numbers[0][1] = 100;
System.out.println(numbers[0][1]);
Output:
100
The element at row 0, column 1 changes from 20 to 100.
Another example:
int[][] marks = {
{70, 80},
{60, 90}
};
marks[1][0] = 85;
System.out.println(marks[1][0]);
Output: 85
Nested loops are commonly used to access every element in a two-dimensional array.
public class Main {
public static void main(String[] args) {
int[][] numbers = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers[i].length; j++) {
System.out.print(numbers[i][j] + " ");
}
System.out.println();
}
}
}
Output:
1 2 3
4 5 6
7 8 9
The outer loop visits each row. The inner loop visits every element in that row.
The length property gives the number of rows.
Each row has its own length.
int[][] numbers = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};
System.out.println(numbers.length);
System.out.println(numbers[0].length);
System.out.println(numbers[1].length);
Output:
3
3
3
numbers.length returns the number of rows.numbers[0].length returns the number of elements in row 0.numbers[i].length when processing each row.
This also works when different rows have different lengths.
A two-dimensional array can store marks for several students across different subjects.
public class Main {
public static void main(String[] args) {
int[][] marks = {
{80, 75, 90},
{65, 85, 70},
{90, 95, 88}
};
for (int i = 0; i < marks.length; i++) {
System.out.print("Student " + (i + 1) + ": ");
for (int j = 0; j < marks[i].length; j++) {
System.out.print(marks[i][j] + " ");
}
System.out.println();
}
}
}
Output:
Student 1: 80 75 90
Student 2: 65 85 70
Student 3: 90 95 88
Each row represents one student's marks in three subjects.
Calculate the total of all values in a two-dimensional array.
public class Main {
public static void main(String[] args) {
int[][] numbers = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int sum = 0;
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers[i].length; j++) {
sum += numbers[i][j];
}
}
System.out.println("Total = " + sum);
}
}
Output:
Total = 45
The nested loops visit each element and add it to the running total.
Find the largest value in a two-dimensional array.
public class Main {
public static void main(String[] args) {
int[][] numbers = {
{10, 25, 30},
{45, 15, 60},
{20, 80, 35}
};
int largest = numbers[0][0];
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers[i].length; j++) {
if (numbers[i][j] > largest) {
largest = numbers[i][j];
}
}
}
System.out.println("Largest = " + largest);
}
}
Output:
Largest = 80
This example assumes the array has at least one row and that the first row contains at least one element.
A jagged array is a two-dimensional array whose rows can have different lengths.
int[][] numbers = new int[3][];
numbers[0] = new int[] {10, 20};
numbers[1] = new int[] {30, 40, 50};
numbers[2] = new int[] {60};
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers[i].length; j++) {
System.out.print(numbers[i][j] + " ");
}
System.out.println();
}
Output:
10 20
30 40 50
60
Each row is a separate array, so its length can be different. The row must be initialized before accessing its elements.
A three-dimensional array adds another level of indexing. It can be thought of as a collection of two-dimensional arrays.
int[][][] numbers = {
{
{1, 2},
{3, 4}
},
{
{5, 6},
{7, 8}
}
};
System.out.println(numbers[0][0][0]);
System.out.println(numbers[1][1][1]);
Output:
1
8
The indexes represent:
Write Java programs to complete these assignments.
1. How do you access the first element in the first row of a 2D array named numbers?
2. What does numbers.length return for a 2D array?
3. What is a jagged array?