Multidimensional Arrays in Java
A multidimensional array in Java is an array of arrays, where each element can hold another
array, forming multiple layers. Java supports two-dimensional (2D) and higher-dimensional
(3D, 4D, etc.) arrays.
1. Declaring a Multidimensional Array
We can declare a multidimensional array using multiple sets of square brackets [][]....
Syntax:
dataType[][]... arrayName;
Example (2D Array):
int[][] matrix; // Declaring a 2D array
Example (3D Array):
int[][][] cube; // Declaring a 3D array
2. Initializing a Multidimensional Array
After declaration, memory must be allocated using new.
Syntax:
arrayName = new dataType[size1][size2]...[sizeN];
Example (2D Array Initialization):
int[][] matrix = new int[3][3]; // 3x3 matrix
Example (3D Array Initialization):
int[][][] cube = new int[2][3][4]; // 2 layers, 3 rows, 4 columns
Alternatively, we can declare and initialize in one step.
Example (2D Array Initialization):
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
}; // 3x3 matrix
Example (3D Array Initialization):
int[][][] cube = {
{
{1, 2, 3}, {4, 5, 6}
},
{
{7, 8, 9}, {10, 11, 12}
}
}; // 2 layers, 2 rows per layer, 3 columns per row
3. Accessing Elements in a Multidimensional Array
Each element is accessed using multiple indices.
Example (Accessing a 2D Array Element):
[Link](matrix[1][2]); // Accesses row 1, column 2 (Value: 6)
Example (Accessing a 3D Array Element):
[Link](cube[1][0][2]); // Accesses layer 1, row 0, column 2
(Value: 9)
4. Modifying Elements in a Multidimensional Array
We can update elements using index notation.
Example:
matrix[2][1] = 15; // Changes row 2, column 1 value to 15
5. Traversing a Multidimensional Array
Using a Nested for Loop (2D Array):
public class Main {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int i = 0; i < [Link]; i++) { // Row loop
for (int j = 0; j < matrix[i].length; j++) { // Column loop
[Link](matrix[i][j] + " ");
}
[Link]();
}
}
}
Output:
1 2 3
4 5 6
7 8 9
Using a Nested for Loop (3D Array):
public class Main {
public static void main(String[] args) {
int[][][] cube = {
{
{1, 2, 3}, {4, 5, 6}
},
{
{7, 8, 9}, {10, 11, 12}
}
};
for (int i = 0; i < [Link]; i++) { // Layer loop
[Link]("Layer " + i);
for (int j = 0; j < cube[i].length; j++) { // Row loop
for (int k = 0; k < cube[i][j].length; k++) { // Column loop
[Link](cube[i][j][k] + " ");
}
[Link]();
}
}
}
}
Output:
Layer 0
1 2 3
4 5 6
Layer 1
7 8 9
10 11 12
6. Finding the Size of a Multidimensional Array
Use length property to find number of rows, columns, and depth.
Example:
int layers = [Link]; // Number of layers
int rows = cube[0].length; // Number of rows in first layer
int cols = cube[0][0].length; // Number of columns in first row of first
layer
[Link]("Layers: " + layers + ", Rows: " + rows + ", Columns: " +
cols);
7. Advantages of Using Multidimensional Arrays
✅ Efficient Organization – Stores structured data efficiently.
✅ Fast Access – Uses index-based retrieval.
✅ Ideal for Matrices – Used in image processing, graphs, and simulations.
8. Limitations of Multidimensional Arrays
⚠ Memory Usage – Takes more space than 1D arrays.
⚠ Complexity – Traversal and manipulation require nested loops.
⚠ Fixed Size – Cannot be resized dynamically.
9. Example Applications
Matrix Addition (2D Arrays)
public class Main {
public static void main(String[] args) {
int[][] A = {
{1, 2},
{3, 4}
};
int[][] B = {
{5, 6},
{7, 8}
};
int[][] sum = new int[2][2];
// Adding matrices
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
sum[i][j] = A[i][j] + B[i][j];
}
}
// Printing the sum matrix
for (int[] row : sum) {
for (int num : row) {
[Link](num + " ");
}
[Link]();
}
}
}
Output:
6 8
10 12
Conclusion
A multidimensional array stores structured data in layers.
2D arrays are commonly used for tables and matrices.
3D and higher-dimensional arrays are useful for games, simulations, and complex
data storage.
Use nested loops for traversal and manipulation.