
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Multi-Dimensional Arrays Support
A multi-dimensional array is nothing but an array of arrays, and yes, it is supported by the Java programming language. It is used to store data within a table, grid, or matrix having rows and columns.
In Java, these arrays are also called ragged arrays or jagged arrays. Let's understand why Java supports multi-dimensional arrays and their types.
Uses of Multidimensional Arrays
Multidimensional arrays in Java are used for various purposes, including:
- Storing data in a tabular format, such as matrices, chessboards, or spreadsheets.
- Representing complex relationships in data like 3D models.
- Managing multiple sets of data, like scores of students in different subjects.
- Building dynamic structures like grids in games and simulations.
Types of Multi-Dimensional Arrays in Java
In Java, multidimensional arrays can be mainly of two types:
- Two-Dimensional Array
- Three-Dimensional Array and Higher
2D Array in Java
A two-dimensional array in Java is represented as an array of one-dimensional arrays of the same type. Mostly, it is used to represent a table of values with rows and columns.
Syntax
This is the syntax to declare a 2D array in Java:
datatype[][] nameOfarray; Or, datatype[][] nameOfarray = new datatype[rowSize][columnSize];
The syntax to declare and initialize a 2D array in Java:
datatype[][] nameOfarray = {{comma separated values}, {comma separated values}};
Example
Let's see an example of 2D array:
public class Example { public static void main(String[] args) { int[][] array2d = { {10, 20, 30}, {11, 21, 31}, {12, 22, 32} }; // rows for(int i = 0; i < array2d.length; i++) { // columns for(int j = 0; j < array2d[i].length; j++) { System.out.print(array2d[i][j] + " "); } System.out.println(); } } }
Output of the above code is as follows:
10 20 30 11 21 31 12 22 32
The above 2D array can be represented in tabular form as:

In short, a two-dimensional array contains one-dimensional arrays of elements. It is represented by two indices, where the first index denotes the position of the array and the second index represents the position of the element within that particular array.
Accessing value at row 1 and column 2 will look like this:

3D Array in Java
Java also supports arrays with more than two levels, such as 3D, 4D, and so on. These are arrays of arrays of arrays.
Syntax
This is the syntax to declare a 3D array in Java:
datatype[][][] nameOfArray; Or, datatype[][][] nameOfArray = new datatype[depthSize][rowSize][columnSize];
The syntax to declare and initialize a 3D array in Java:
datatype[][][] nameOfArray = { { {comma separated values}, {comma separated values} }, { {comma separated values}, {comma separated values} } };
Example
The following Java program is an example of the 3D array:
public class Example { public static void main(String[] args) { // a 3D array int[][][] array3d = { { {10, 20, 30}, {11, 21, 31}, {12, 22, 32} }, { {40, 50, 60}, {41, 51, 61}, {42, 52, 62} } }; for(int i = 0; i < array3d.length; i++) { System.out.println("Layer " + (i + 1) + ":"); // Rows for(int j = 0; j < array3d[i].length; j++) { // Columns for(int k = 0; k < array3d[i][j].length; k++) { System.out.print(array3d[i][j][k] + " "); } System.out.println(); } System.out.println(); } } }
Output of the above code is as follows:
Layer 1: 10 20 30 11 21 31 12 22 32 Layer 2: 40 50 60 41 51 61 42 52 62