Day 10: Multi-Dimensional Arrays in Java (With Practical Examples)
When working with structured data such as tables, grids, or matrices, one-dimensional arrays are no longer enough. Java provides multi-dimensional arrays, allowing us to organize data in rows and columns efficiently.
Get Yodgorbek Komilov’s stories in your inbox
Join Medium for free to get updates from this writer.
In this article, we’ll explore 2D arrays in Java, understand how they work internally, and walk through practical examples you’ll actually encounter in real projects and interviews.
Understanding Multi-Dimensional Arrays
A multi-dimensional array in Java is essentially an array of arrays.
The most commonly used type is the two-dimensional (2D) array, which can be visualized as a grid:
[ [1, 2, 3],
[4, 5, 6],
[7, 8, 9] ]Each inner array represents a row, and each element inside it represents a column value.
Declaring and Initializing 2D Arrays
Basic Declaration
int[][] matrix;Allocation with Fixed Size
int[][] matrix = new int[3][3];At this point, all elements are initialized to 0.
Initialization with Values
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};Accessing and Updating Elements
Elements are accessed using two indices:
int element = matrix[2][1]; // Output: 8Updating a value:
matrix[0][0] = 99;Example 1: Printing a Matrix
public class PrintMatrix {
public static void main(String[] args) {
int[][] matrix = {
{1, 2},
{3, 4},
{5, 6}
};
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}Output
1 2
3 4
5 6Example 2: Sum of All Elements in a 2D Array
public class Sum2DArray {
public static void main(String[] args) {
int[][] numbers = {
{1, 2, 3},
{4, 5, 6}
};
int sum = 0;
for (int[] row : numbers) {
for (int num : row) {
sum += num;
}
}
System.out.println("Sum = " + sum);
}
}Output
Sum = 21Example 3: Finding the Largest Element
public class MaxElement2D {
public static void main(String[] args) {
int[][] data = {
{12, 7, 9},
{3, 15, 6},
{10, 2, 8}
};
int max = data[0][0];
for (int[] row : data) {
for (int value : row) {
if (value > max) {
max = value;
}
}
}
System.out.println("Maximum value: " + max);
}
}Jagged Arrays: Rows with Different Lengths
Java allows jagged arrays, meaning each row can have a different number of columns.
int[][] jaggedArray = {
{1, 2, 3},
{4, 5},
{6}
};Traversing a Jagged Array
for (int i = 0; i < jaggedArray.length; i++) {
for (int j = 0; j < jaggedArray[i].length; j++) {
System.out.print(jaggedArray[i][j] + " ");
}
System.out.println();
}Example 4: Matrix Addition
public class MatrixAddition {
public static void main(String[] args) {
int[][] a = {
{1, 2},
{3, 4}
};
int[][] b = {
{5, 6},
{7, 8}
};
int[][] sum = new int[2][2];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}
for (int[] row : sum) {
for (int value : row) {
System.out.print(value + " ");
}
System.out.println();
}
}
}Output
6 8
10 12Common Mistakes Developers Make
❌ Using Wrong Indexes
matrix[3][0]; // ArrayIndexOutOfBoundsException❌ Assuming All Rows Have Equal Length
matrix[0].length == matrix[1].length // Not always trueTime Complexity
For a 2D array with n rows and m columns:
- Traversal: O(n × m)
- Accessing an element: O(1)
Real-World Use Cases
- Game boards (Tic-Tac-Toe, Chess)
- Image pixel processing
- Excel-like data tables
- Dynamic programming algorithms
- Heat maps and grids
Key Takeaways
- A 2D array in Java is an array of arrays
- Java supports jagged arrays by design
- Nested loops are essential for traversal
- 2D arrays are widely used in interviews and real projects
Final Thoughts
Mastering multi-dimensional arrays is a core milestone in learning Java.
They appear frequently in coding interviews, competitive programming, and real-world systems.




