Multidimensional Array

A multidimensional array is those array that stores the element in the column-based index  (know as matrix )and row or in table form.

multidimensional array declaration
matrix form of multidimensional array

There are two types of Multidimensional array:

types of array java

Let’s compare the single-dimension array and 2d array for better understand 

in the single-dimension array, a reference variable refers to an array in this element of the array  don’t refer another array

Example

Example of array declaration in java

In 2d array, a reference variable refers to an array in this element of the array  refer  another array

Example

int[][] temp = {
      {1, 2, 3}, //an arrya
      {4, 5, 6}, //an arrya
      {7,10,44}, //an aray
};

Here  temp is a 2d array

Structure of array
memory representation of array java

How to Declare  2d array:

These are the following way to declair 2d array

1.	dataType[][] arrayRefVar; (or)  
2.	dataType [][]arrayRefVar; (or)  
3.	dataType arrayRefVar[][]; (or)  
4.	dataType []arrayRefVar[];  

Initialization of array:

initialization of array

Example of multidimensional array

Let’s see the simple example to declare, instantiate, initialize and print the 2Dimensional array.

1.	//Java Program to illustrate the use of multidimensional array  
2.	class Testarray3{  
3.	public static void main(String args[]){  
4.	//declaring and initializing 2D array  
5.	int arr[][]={{1,2,3},{2,4,5},{4,4,5}};  
6.	//printing 2D array  
7.	for(int i=0;i<3;i++){  
8.	 for(int j=0;j<3;j++){  
9.	   System.out.print(arr[i][j]+" ");  
10.	 }  
11.	 System.out.println();  
12.	}  
13.	} 
14. }
1 2 3
2 4 5
4 4 5

3d array in java :

First see the following example of 3d array.

Example

//test is a 3d array int[2][3][3] 
int[][][] test = {
        {
          {1, -2, 3}, 
          {2, 3, 4}
          {11, 12, 13}
        }, 

        { 
          {-4, -5, 6}, 
          {1, 7, 8}, 
          {2, 3, 9}
        } 
};

   In this test is a reference variable it contains 3 elements
3d array is the array of 2d array

Structure of These 3 elements are

Structure of These 3 elements are java


SHARE THIS PAGE





Important Link