dsa

Multi-Dimensional Arrays

In this tutorial, we will learn about the Java multidimensional array using 2-dimensional arrays and 3-dimensional arrays with the help of examples.

Multi-dimensional arrays are an extended form of one-dimensional arrays and are frequently used to store data for mathematic computations, image processing, and record management.

A multi-dimensional array is a collection of one-dimensional arrays and can represent relational tables and matrices. Both matrices and tables can be seen as a collection of rows that can be mapped into an array of rows (a one-dimensional array). Multi-dimensional arrays store values in row-major order, meaning that elements of all rows are stored in a consecutive (one row after the another) manner. A real-life example of multi-dimensional arrays would be the total number of units sold every day, of every week in a month at a store.

For example,

int[][] a = new int[3][4];

Here, we have created a multidimensional array named a. It is a 2-dimensional array, that can hold a maximum of 12 elements,

Two – dimensional Array (2D-Array)

Two – dimensional array is the simplest form of a multidimensional array. A two – dimensional array can be seen as an array of one – dimensional array for easier understanding.

Indirect Method of Declaration:

Declaration – Syntax:

data_type[][] array_name = new data_type[x][y];
          For example: int[][] arr = new int[120][240];
         //a 2D array or matrix

Initialization – Syntax:

array_name[row_index][column_index] = value;
        For example: arr[0][0] = 1;

Example of Multidimensional 2D Java Array

class GFG { 
	public static void main(String[] args) 
	{ 

		int[][] arr = new int[10][20]; 
		arr[0][0] = 65; 

		System.out.println("arr[0][0] = " + arr[0][0]); 
	} 
} 
   

   output:
           arr[0][0] = 65
   

Example: Print all elements of 2d array Using Loop

class MultidimensionalArray {
    public static void main(String[] args) {

        int[][] a = {
            {1, -2, 3}, 
            {-4, -5, 5, 9}, 
            {7}, 
        };
      
        for (int i = 0; i < a.length; ++i) {
            for(int j = 0; j < a[i].length; ++j) {
                System.out.println(a[i][j]);
            }
        }
    }
}


output:
1
-2
3
-4
-5
5
9
7

   
two-d
2D array

Three – dimensional Array (3D-Array)

Three – dimensional array is a complex form of a multidimensional array. A three – dimensional array can be seen as an array of two – dimensional array for easier understanding.

Indirect Method of Declaration:

Declaration – Syntax:

data_type[][][] array_name = new data_type[x][y][z];
          For example: int[][][] arr = new int[120][240][1324];
         //a 3D array or matrix

Initialization – Syntax:

array_name[row_index][column_index] = value;
        For example: arr[0][0][0] = 6;

Example of Multidimensional 3D Java Array

class GFG { 
	public static void main(String[] args) 
	{ 

		int[][] arr = new int[10][20][23]; 
		arr[0][0][0] = 65; 

		System.out.println("arr[0][0][0]= " + arr[0][0][0]); 
	} 
} 
   

   output:
           arr[0][0][] = 65