Multi-dimensional arrays in Java are basically arrays of arrays. It allows us to store data in a tabular or grid-like structure, making them useful for scenarios like matrices, game boards, or any data that requires multiple dimensions. It is useful when we want to store data in tabular form (rows and columns).
It can be a two-dimensional (2D) or three-dimensional (3D) array.
Two-Dimensional (2D) Array
It is the simplest multi-dimensional array having rows and columns.
Declaration
- DataType[][] arrayName;
or
- DataType arrayName[][];
or
- arrayName = new DataType[rows][columns];
We can declare and initialize a two-dimensional array in a single statement as follows.
- datatype[][] arrayName = new datatype[rows][columns];
Assigning Values
- arrayName[rows][columns] = {values};
Examples
- // 2D Array with two rows and two columns Intialised and Assigned
- int[][] arr = { { 1, 2 }, { 3, 4 } };
- //array with three rows and three columns
- int[][] arr = new int[3][3];
Representation of 2D Array

Note that Java uses zero-based indexing. It means the indexing of arrays in Java starts with 0.
Also, note that in a multi-dimensional array, each row can be of different sizes. It means that each row may not have the same number of elements.
Two-Dimensional (2D) Array Java Program
Example
- public class Main {
- public static void main(String args[]) {
- int arr[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; // 3×3 matrix
- // Printing the 2D array
- for (int i = 0; i < 3; i++) //loop for rows
- {
- for (int j = 0; j < 3; j++) //loop for columns
- {
- System.out.print(arr[i][j] + ” “); //prints element with space
- }
- System.out.println(); //throws cursor to the next line
- }
- }
- }
Compile and Run
Output:
1 2 3
4 5 6
7 8 9
Explanation
In the above program, we have declared and initialized a 3×3 matrix. The first for loop iterates over the rows, and the second for loop iterates over the columns. The first print statement prints the elements of the array with space, and the second print statement throws the cursor to the next line.
Two-Dimensional (2D) Array with User Input
Example
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // Define the dimensions of the array
- System.out.print(“Enter the number of rows:”);
- int rows = scanner.nextInt(); //reading number of rows from the user
- System.out.print(“Enter the number of columns:”);
- int columns = scanner.nextInt(); //reading number of columns from the user
- // Initialize the 2D array
- int[][] array = new int[rows][columns];
- //reading array elements from the user
- System.out.println(“Enter the elements of the array:”);
- for (int i = 0; i < rows; i++) //loop for rows
- {
- for (int j = 0; j < columns; j++) //loop for columns
- {
- System.out.print(“Enter element for position (” + i + “, ” + j + “): “);
- //reading array elements one by one
- array[i][j] = scanner.nextInt();
- }
- }
- // Printing the 2D array
- System.out.println(“The entered 2D array is:”);
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- System.out.print(array[i][j] + ” “);
- }
- System.out.println();
- }
- scanner.close();
- }
- }
Output:
Enter the number of rows:3
Enter the number of columns:3
Enter the elements of the array:
Enter element for position (0, 0): 1
Enter element for position (0, 1): 2
Enter element for position (0, 2): 3
Enter element for position (1, 0): 4
Enter element for position (1, 1): 5
Enter element for position (1, 2): 6
Enter element for position (2, 0): 7
Enter element for position (2, 1): 8
Enter element for position (2, 2): 9
The entered 2D array is:
1 2 3
4 5 6
7 8 9
Three-Dimensional (3D) Array
It is a complex form of a 2D array. In other words, it is an array of a two-dimensional array.
Declaration
- DataType[][][] arrayName;
or
- DataType arrayName[][][];
or
- arrayName = new DataType[][][];
We can declare and initialize a three-dimensional array in a single statement as follows.
- datatype[][][] arrayName = new datatype[][][];
Assigning Values
- arrayName[][][] = {values};
Examples
public class Main {
public static void main(String[] args)
{
//declaring and initializing three-dimensional array
int[][][] threeDArray = {
{
{1, 2, 3},
{4, 5, 6}
},
{
{7, 8, 9},
{10, 11, 12}
}
};
//Print the elements of the 3D array
System.out.println("Elements of the 3D Array:");
for (int i = 0; i < threeDArray.length; i++) {
for (int j = 0; j < threeDArray[i].length; j++) {
for (int k = 0; k < threeDArray[i][j].length; k++) {
System.out.print(threeDArray[i][j][k] + " ");
}
System.out.println(); // Move to the next line for better readability
}
System.out.println(); // Add a blank line between blocks
}
}
}
- // 3D Array with two rows ad two columns Intialised and Assigned
- int[][][] values = {{{1, 2, 3}, {4, 5, 6}}, {{-2, -8, 3, 7}, {5}, {12, 11}}};
- //array contains three array of three rows and three columns
- int[][][] arr = new int[3][3][3];
Representation of 3D Array

Three-Dimensional (3D) Array Java Program
Example
Compile and Run
Output:
Elements of the 3D Array:
1 2 3
4 5 6
7 8 9
10 11 12
Size of Multidimensional Arrays
We can calculate the size of the array by multiplying the size of all the dimensions. It gives the number of elements that can be stored in an array. For example, int[][][] num = new int[5][5][5]; can store total 125 elements.
Why Use Multidimensional Arrays?
Multidimensional arrays are useful in many real-world scenarios, such as:
- Mathematical computations: Representing matrices for algebraic calculations.
- Game development: Storing board layouts, game grids, or levels.
- Image processing: Representing pixels in a digital image.
- Data analysis: Managing structured data like spreadsheets or databases.
Conclusion
Multidimensional arrays in Java serve as an effective solution for managing organized data structures while processing them. Multidimensional arrays surpass the functionality of single-dimensional arrays because they enable table organization, which results in applications ranging from scientific computing to game development.
Two-dimensional arrays function as the preferred form for applications that need matrix computations together with pathfinding implementations and data presentation. The option to produce jagged arrays makes the process more adaptable for developers since they can handle irregular data formats.
Leave a Reply