Multi-Dimensional Array in Java
Introduction
A Java two-dimensional array is an array whose elements refer to other arrays. We can use it for rows of marks or a simple seating plan. Each row is a separate array, so rows can have different lengths.
The most commonly used multi-dimensional array is a two-dimensional array, which can be visualized as rows and columns.
For example, a matrix can be represented as:
Column
0 1 2
+----+----+----+
Row 0 | 10 | 20 | 30 |
+----+----+----+
Row 1 | 40 | 50 | 60 |
+----+----+----+
Row 2 | 70 | 80 | 90 |
+----+----+----+In Java, we can create this using:
int[][] numbers = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};Here:
numbersis a two-dimensional array.- It contains 3 rows.
- Each row contains 3 elements.
- The first index represents the row.
- The second index represents the column.
For example:
numbers[0][1]returns:
20What is a Multi-Dimensional Array?
A multi-dimensional array is an array with more than one dimension.
Java supports arrays with multiple dimensions, such as:
int[][] array2D;
int[][][] array3D;The most common type is the two-dimensional array.
int[][] numbers;A two-dimensional array can be visualized as:
Column
0 1 2
+----+----+----+
0 | 10 | 20 | 30 |
+----+----+----+
1 | 40 | 50 | 60 |
+----+----+----+
2 | 70 | 80 | 90 |
+----+----+----+
RowThe first index represents the row:
numbers[1]The second index represents the column:
numbers[1][2]Why Do We Need Multi-Dimensional Arrays?
A single-dimensional array is useful for storing data in a sequence.
For example:
int[] marks = {80, 75, 90, 85};But sometimes data naturally belongs to rows and columns.
For example, student marks for different subjects:
Java SQL Spring
Student 1 80 75 90
Student 2 85 88 92
Student 3 70 80 75This type of data can be represented using a two-dimensional array:
int[][] marks = {
{80, 75, 90},
{85, 88, 92},
{70, 80, 75}
};Declaring a Two-Dimensional Array
The syntax is:
dataType[][] arrayName;Example
int[][] numbers;Another valid syntax is:
int numbers[][];The following form is generally easier to read:
int[][] numbers;Creating a Two-Dimensional Array
We can create a two-dimensional array using the new keyword.
int[][] numbers = new int[3][4];This creates:
3 rows
4 columnsSo the total number of elements is:
3 × 4 = 12The structure can be visualized as:
0 1 2 3
+----+----+----+----+
0 | 0 | 0 | 0 | 0 |
+----+----+----+----+
1 | 0 | 0 | 0 | 0 |
+----+----+----+----+
2 | 0 | 0 | 0 | 0 |
+----+----+----+----+Program 1: Creating a Two-Dimensional Array
public class HelloWorld {
public static void main(String[] args) {
int[][] numbers = new int[3][4];
System.out.println("Rows: " + numbers.length);
System.out.println("Columns: " + numbers[0].length);
}
}Output
Rows: 3
Columns: 4Here:
numbers.lengthreturns the number of rows.
And:
numbers[0].lengthreturns the number of columns in the first row.
Initializing a Two-Dimensional Array
We can assign values using row and column indexes.
int[][] numbers = new int[2][3];
numbers[0][0] = 10;
numbers[0][1] = 20;
numbers[0][2] = 30;
numbers[1][0] = 40;
numbers[1][1] = 50;
numbers[1][2] = 60;The array looks like:
0 1 2
+----+----+----+
0 | 10 | 20 | 30 |
+----+----+----+
1 | 40 | 50 | 60 |
+----+----+----+Program 2: Initializing a Two-Dimensional Array
public class HelloWorld {
public static void main(String[] args) {
int[][] numbers = new int[2][3];
numbers[0][0] = 10;
numbers[0][1] = 20;
numbers[0][2] = 30;
numbers[1][0] = 40;
numbers[1][1] = 50;
numbers[1][2] = 60;
System.out.println(numbers[0][0]);
System.out.println(numbers[0][1]);
System.out.println(numbers[0][2]);
System.out.println(numbers[1][0]);
System.out.println(numbers[1][1]);
System.out.println(numbers[1][2]);
}
}Output
10
20
30
40
50
60Initializing a Two-Dimensional Array Using Array Literals
When values are already known, we can initialize the array directly.
int[][] numbers = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};This creates a two-dimensional array with:
3 rows
3 columnsProgram 3: Array Literal Initialization
public class HelloWorld {
public static void main(String[] args) {
int[][] numbers = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};
System.out.println(numbers[0][0]);
System.out.println(numbers[1][1]);
System.out.println(numbers[2][2]);
}
}Output
10
50
90Understanding Row and Column Index
Consider:
int[][] numbers = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};The indexes are:
Column
0 1 2
+----+----+----+
Row 0 | 10 | 20 | 30 |
+----+----+----+
Row 1 | 40 | 50 | 60 |
+----+----+----+
Row 2 | 70 | 80 | 90 |
+----+----+----+For example:
numbers[0][0]means:
Row 0, Column 0and returns:
10Similarly:
numbers[2][1]means:
Row 2, Column 1and returns:
80Program 4: Accessing Elements
public class HelloWorld {
public static void main(String[] args) {
int[][] numbers = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};
System.out.println("First element: " + numbers[0][0]);
System.out.println("Middle element: " + numbers[1][1]);
System.out.println("Last element: " + numbers[2][2]);
}
}Output
First element: 10
Middle element: 50
Last element: 90Modifying Elements in a Two-Dimensional Array
We can modify an element using its row and column indexes.
numbers[1][1] = 500;If the original array is:
10 20 30
40 50 60
70 80 90after:
numbers[1][1] = 500;it becomes:
10 20 30
40 500 60
70 80 90Program 5: Modifying an Element
public class HelloWorld {
public static void main(String[] args) {
int[][] numbers = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};
System.out.println("Before: " + numbers[1][1]);
numbers[1][1] = 500;
System.out.println("After: " + numbers[1][1]);
}
}Output
Before: 50
After: 500Traversing a Two-Dimensional Array
To visit every element of a two-dimensional array, we commonly use nested loops.
A nested loop means one loop is placed inside another loop.
For example:
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers[i].length; j++) {
System.out.println(numbers[i][j]);
}
}The outer loop handles the rows.
The inner loop handles the columns.
Program 6: Traversing Using Nested for Loop
public class HelloWorld {
public static void main(String[] args) {
int[][] numbers = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};
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
70 80 90How Nested Loops Work
For this array:
10 20 30
40 50 60
70 80 90the outer loop starts with:
i = 0The inner loop processes:
numbers[0][0]
numbers[0][1]
numbers[0][2]Then the outer loop moves to:
i = 1The inner loop processes:
numbers[1][0]
numbers[1][1]
numbers[1][2]Finally:
i = 2processes:
numbers[2][0]
numbers[2][1]
numbers[2][2]Traversing Using Enhanced for Loop
A two-dimensional array can also be traversed using nested enhanced for loops.
for (int[] row : numbers) {
for (int value : row) {
System.out.print(value + " ");
}
System.out.println();
}The outer loop gets one row at a time.
The inner loop gets each element from that row.
Program 7: Enhanced for Loop
public class HelloWorld {
public static void main(String[] args) {
int[][] numbers = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};
for (int[] row : numbers) {
for (int value : row) {
System.out.print(value + " ");
}
System.out.println();
}
}
}Output
10 20 30
40 50 60
70 80 90Finding the Number of Rows
The length property of a two-dimensional array gives the number of rows.
int[][] numbers = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};
System.out.println(numbers.length);Output:
3Finding the Number of Columns
For a regular two-dimensional array, we can use:
numbers[0].lengthExample
System.out.println(numbers[0].length);Output:
3So:
numbers.length → number of rows
numbers[0].length → number of columns in row 0For a rectangular array where every row has the same number of columns, this gives the column count.
Program 8: Find Rows and Columns
public class HelloWorld {
public static void main(String[] args) {
int[][] numbers = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};
int rows = numbers.length;
int columns = numbers[0].length;
System.out.println("Rows = " + rows);
System.out.println("Columns = " + columns);
}
}Output
Rows = 3
Columns = 3Taking Two-Dimensional Array Input
We can use Scanner to read values from the user.
For example, if we want 2 rows and 3 columns:
int[][] numbers = new int[2][3];Then we can use nested loops to read each value.
Program 9: Read a Two-Dimensional Array from User
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int rows = scanner.nextInt();
System.out.print("Enter number of columns: ");
int columns = scanner.nextInt();
int[][] numbers = new int[rows][columns];
System.out.println("Enter array elements:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
numbers[i][j] = scanner.nextInt();
}
}
System.out.println("Array:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(numbers[i][j] + " ");
}
System.out.println();
}
scanner.close();
}
}Example Input
Enter number of rows: 2
Enter number of columns: 3
Enter array elements:
10
20
30
40
50
60Output
Array:
10 20 30
40 50 60Program 10: Find the Sum of All Elements
We can calculate the sum using nested loops.
public class HelloWorld {
public static void main(String[] args) {
int[][] numbers = {
{10, 20, 30},
{40, 50, 60}
};
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("Sum = " + sum);
}
}Output
Sum = 210Calculation:
10 + 20 + 30 + 40 + 50 + 60 = 210Program 11: Find the Largest Element
public class HelloWorld {
public static void main(String[] args) {
int[][] numbers = {
{10, 45, 30},
{80, 20, 60},
{35, 90, 15}
};
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 = 90We start with:
int largest = numbers[0][0];Then every element is compared with the current largest value.
Program 12: Find the Smallest Element
public class HelloWorld {
public static void main(String[] args) {
int[][] numbers = {
{10, 45, 30},
{80, 20, 60},
{35, 90, 15}
};
int smallest = numbers[0][0];
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers[i].length; j++) {
if (numbers[i][j] < smallest) {
smallest = numbers[i][j];
}
}
}
System.out.println("Smallest = " + smallest);
}
}Output
Smallest = 10Program 13: Find the Sum of Each Row
Sometimes we need to calculate the sum separately for each row.
public class HelloWorld {
public static void main(String[] args) {
int[][] numbers = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};
for (int i = 0; i < numbers.length; i++) {
int sum = 0;
for (int j = 0; j < numbers[i].length; j++) {
sum += numbers[i][j];
}
System.out.println(
"Sum of Row " + i + " = " + sum
);
}
}
}Output
Sum of Row 0 = 60
Sum of Row 1 = 150
Sum of Row 2 = 240Program 14: Find the Sum of Each Column
We can also calculate the sum of each column.
public class HelloWorld {
public static void main(String[] args) {
int[][] numbers = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};
int rows = numbers.length;
int columns = numbers[0].length;
for (int j = 0; j < columns; j++) {
int sum = 0;
for (int i = 0; i < rows; i++) {
sum += numbers[i][j];
}
System.out.println(
"Sum of Column " + j + " = " + sum
);
}
}
}Output
Sum of Column 0 = 120
Sum of Column 1 = 150
Sum of Column 2 = 180Program 15: Search for an Element
We can search for an element using nested loops.
public class HelloWorld {
public static void main(String[] args) {
int[][] numbers = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};
int search = 50;
boolean found = false;
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers[i].length; j++) {
if (numbers[i][j] == search) {
System.out.println(
"Element found at row " + i +
", column " + j
);
found = true;
break;
}
}
if (found) {
break;
}
}
if (!found) {
System.out.println("Element not found");
}
}
}Output
Element found at row 1, column 1Program 16: Print the Main Diagonal
Consider the following square matrix:
10 20 30
40 50 60
70 80 90The main diagonal is:
10
50
90The indexes are:
[0][0]
[1][1]
[2][2]So the row index and column index are the same.
public class HelloWorld {
public static void main(String[] args) {
int[][] matrix = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};
System.out.println("Main Diagonal:");
for (int i = 0; i < matrix.length; i++) {
System.out.println(matrix[i][i]);
}
}
}Output
Main Diagonal:
10
50
90Program 17: Find the Sum of Main Diagonal
public class HelloWorld {
public static void main(String[] args) {
int[][] matrix = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};
int sum = 0;
for (int i = 0; i < matrix.length; i++) {
sum += matrix[i][i];
}
System.out.println("Diagonal Sum = " + sum);
}
}Output
Diagonal Sum = 150Calculation:
10 + 50 + 90 = 150Program 18: Print the Secondary Diagonal
For the matrix:
10 20 30
40 50 60
70 80 90the secondary diagonal is:
30
50
70The indexes are:
[0][2]
[1][1]
[2][0]The column index can be calculated using:
column = length - 1 - rowExample
public class HelloWorld {
public static void main(String[] args) {
int[][] matrix = {
{10, 20, 30},
{40, 50, 60},
{70, 80, 90}
};
int size = matrix.length;
System.out.println("Secondary Diagonal:");
for (int i = 0; i < size; i++) {
System.out.println(
matrix[i][size - 1 - i]
);
}
}
}Output
Secondary Diagonal:
30
50
70Program 19: Add Two Matrices
Two matrices of the same dimensions can be added element by element.
Consider:
Matrix A:
10 20
30 40and:
Matrix B:
1 2
3 4Their sum is:
11 22
33 44Program:
public class HelloWorld {
public static void main(String[] args) {
int[][] first = {
{10, 20},
{30, 40}
};
int[][] second = {
{1, 2},
{3, 4}
};
int rows = first.length;
int columns = first[0].length;
int[][] result = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
result[i][j] =
first[i][j] + second[i][j];
}
}
System.out.println("Result:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
}Output
Result:
11 22
33 44Program 20: Transpose of a Matrix
The transpose of a matrix is created by converting rows into columns and columns into rows.
Original matrix:
10 20 30
40 50 60Transpose:
10 40
20 50
30 60Program:
public class HelloWorld {
public static void main(String[] args) {
int[][] matrix = {
{10, 20, 30},
{40, 50, 60}
};
int rows = matrix.length;
int columns = matrix[0].length;
int[][] transpose = new int[columns][rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
transpose[j][i] = matrix[i][j];
}
}
System.out.println("Transpose:");
for (int i = 0; i < transpose.length; i++) {
for (int j = 0; j < transpose[i].length; j++) {
System.out.print(transpose[i][j] + " ");
}
System.out.println();
}
}
}Output
Transpose:
10 40
20 50
30 60Jagged Arrays in Java
Java does not require every row of a two-dimensional array to have the same number of columns.
Such an array is called a jagged array.
For example:
int[][] numbers = {
{10, 20},
{30, 40, 50},
{60, 70, 80, 90}
};Here:
Row 0 → 2 elements
Row 1 → 3 elements
Row 2 → 4 elementsThe structure is:
10 20
30 40 50
60 70 80 90Program 21: Jagged Array
public class HelloWorld {
public static void main(String[] args) {
int[][] numbers = {
{10, 20},
{30, 40, 50},
{60, 70, 80, 90}
};
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 70 80 90Notice that we use:
numbers[i].lengthinstead of assuming that every row has the same number of columns.
Creating a Jagged Array Using new
We can also create a jagged array step by step.
int[][] numbers = new int[3][];
numbers[0] = new int[2];
numbers[1] = new int[3];
numbers[2] = new int[4];Now:
Row 0 → 2 elements
Row 1 → 3 elements
Row 2 → 4 elementsProgram 22: Creating a Jagged Array
public class HelloWorld {
public static void main(String[] args) {
int[][] numbers = new int[3][];
numbers[0] = new int[2];
numbers[1] = new int[3];
numbers[2] = new int[4];
numbers[0][0] = 10;
numbers[0][1] = 20;
numbers[1][0] = 30;
numbers[1][1] = 40;
numbers[1][2] = 50;
numbers[2][0] = 60;
numbers[2][1] = 70;
numbers[2][2] = 80;
numbers[2][3] = 90;
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 70 80 90Java Multi-Dimensional Arrays Are Arrays of Arrays
An important concept is that Java's two-dimensional arrays are technically arrays whose elements are themselves arrays.
Consider:
int[][] numbers = new int[3][4];Conceptually:
numbers
|
+---- Row 0 → [0][0] [0][1] [0][2] [0][3]
|
+---- Row 1 → [1][0] [1][1] [1][2] [1][3]
|
+---- Row 2 → [2][0] [2][1] [2][2] [2][3]This is also why Java can support jagged arrays.
Each row can be a separate array with a different length.
Three-Dimensional Array
Java also supports arrays with three dimensions.
Syntax:
int[][][] numbers;Example
int[][][] numbers = new int[2][3][4];This can be understood as:
2 blocks
3 rows per block
4 columns per rowA three-dimensional array is useful when data has three logical dimensions.
For example:
Building
↓
Floor
↓
RoomProgram 23: Three-Dimensional Array
public class HelloWorld {
public static void main(String[] args) {
int[][][] numbers = {
{
{1, 2},
{3, 4}
},
{
{5, 6},
{7, 8}
}
};
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers[i].length; j++) {
for (int k = 0; k < numbers[i][j].length; k++) {
System.out.print(numbers[i][j][k] + " ");
}
System.out.println();
}
System.out.println();
}
}
}Output
1 2
3 4
5 6
7 8Each additional dimension requires another level of indexing.
For example:
numbers[i][j][k]Multi-Dimensional Array and Memory
A two-dimensional Java array is not necessarily stored as one single rectangular memory block.
For example:
int[][] numbers = new int[3][3];can be viewed conceptually as:
numbers
|
+---- Row 0 → int[]
|
+---- Row 1 → int[]
|
+---- Row 2 → int[]Each row is itself an array.
This model allows Java to create jagged arrays where different rows have different lengths.
Common Mistakes
Mistake 1: Using the Wrong Index
For:
int[][] numbers = new int[2][3];valid row indexes are:
0
1and valid column indexes are:
0
1
2Therefore:
numbers[2][0]is invalid.
Mistake 2: Confusing Rows and Columns
For:
int[][] numbers = new int[3][4];there are:
3 rows
4 columnsSo:
numbers.lengthreturns:
3while:
numbers[0].lengthreturns:
4Mistake 3: Assuming Every Row Has the Same Length
For a jagged array:
int[][] numbers = {
{10, 20},
{30, 40, 50},
{60, 70, 80, 90}
};do not assume:
numbers[0].length == numbers[1].lengthInstead, use:
numbers[i].lengthwhile traversing each row.
Mistake 4: Using Only One Loop
A two-dimensional array normally requires nested loops to process every element.
For example:
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers[i].length; j++) {
System.out.println(numbers[i][j]);
}
}The outer loop processes rows, while the inner loop processes elements inside each row.
Single Dimensional vs Multi-Dimensional Array
| Feature | Single Dimensional | Multi-Dimensional |
|---|---|---|
| Structure | Single sequence | Multiple dimensions |
| Example | int[] | int[][] |
| Indexes | One | Two or more |
| Common use | List of values | Tables, matrices |
| Traversal | Usually one loop | Usually nested loops |
| Example access | array[2] | array[2][3] |
Example
Single Dimensional
int[] numbers = {10, 20, 30};Access:
numbers[1]Two Dimensional
int[][] numbers = {
{10, 20},
{30, 40}
};Access:
numbers[1][0]Practical Example: Student Marks
Suppose three students have marks in three subjects.
Java SQL Spring
Student 1 80 75 90
Student 2 85 88 92
Student 3 70 80 75We can represent this using:
int[][] marks = {
{80, 75, 90},
{85, 88, 92},
{70, 80, 75}
};Program 24: Student Marks Using Two-Dimensional Array
public class HelloWorld {
public static void main(String[] args) {
int[][] marks = {
{80, 75, 90},
{85, 88, 92},
{70, 80, 75}
};
for (int i = 0; i < marks.length; i++) {
int total = 0;
for (int j = 0; j < marks[i].length; j++) {
total += marks[i][j];
}
double average =
(double) total / marks[i].length;
System.out.println(
"Student " + (i + 1)
+ " Total = " + total
+ ", Average = " + average
);
}
}
}Output
Student 1 Total = 245, Average = 81.66666666666667
Student 2 Total = 265, Average = 88.33333333333333
Student 3 Total = 225, Average = 75.0This is a practical example of using a two-dimensional array to represent related data in rows and columns.
Key Points to Remember
- A multi-dimensional array contains multiple dimensions.
- The most common type is a two-dimensional array.
- A two-dimensional array is declared using:
int[][] numbers;- A two-dimensional array uses two indexes:
numbers[row][column]- Java arrays are zero-indexed.
array.lengthgives the number of rows for a two-dimensional array.array[row].lengthgives the number of elements in a particular row.- Nested loops are commonly used to traverse a two-dimensional array.
- Java supports jagged arrays where rows can have different lengths.
- A two-dimensional Java array is technically an array of arrays.
- Java also supports three-dimensional and higher-dimensional arrays.
- Multi-dimensional arrays are useful for matrices, tables, grids, and other structured data.
ArrayIndexOutOfBoundsExceptioncan occur when an invalid row or column index is accessed.- When working with jagged arrays, use
array[i].lengthinstead of assuming every row has the same length.
See you in next page....🙂