Multidimensional array in java

Let’s learn multidimensional array in java.

Multidimensional array in java

Java multidimensional array is an array of arrays with multiple rows and columns. Data in multidimensional array is stored in the form of rows and columns. Data is accessed using row column index (based on index).

Also read – arrays in java

Syntax:

datatype[1][2]…[nth dimension] arrayname = new datatype[1][2]…[nth size]; 


Two dimensional array java

Java two dimensional array is an array of arrays. It is the simplest form of multidimensional array.

In 2d array java data is stored in rows and columns. To access data or elements in java 2d array we use row index and column index.

Here’s how to declare two dimensional array in java,

// declare 2d array java
datatype[][] arrayName;

Different ways to declare 2d array java
int [ ][ ]b;
int[ ]  [ ]b;
int b[ ][ ];
int[ ] b[ ];

Here’s how to create 2d array in java,

arrayName = new datatype[rowsize][columnsize];

Example: b = new int[3][4]; // 3 rows and 4 columns are created

To initialize 2d array in java,

b[0][0] = 1;

b[0][1] = 2;

b[0][2] = 3;

….

and so on. Also we can declare, create and initialize 2d array in single line like this,

int[ ][ ] b = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};

two dimensional array in java

Let’s see an example,

// 2d array java
public class Demo 
{
   public static void main(String[] args) 
   {
      int[][] num = {
              {45, 48, 65}, // each component of multidimensional array is also an array
              {1, 5, 3, 9}, 
              {2, 6}, 
      };
      // length of each row can be found using "length" attribute
      System.out.println("Row 1 length : " + num[0].length);
      System.out.println("Row 2 length : " + num[1].length);
      System.out.println("Row 3 length : " + num[2].length);
   }
}



Output:

Row 1 length : 3
Row 2 length : 4
Row 3 length : 2


Now let’s see a java program to print all elements of java two dimensional array,

public class Demo 
{
   public static void main(String[] args) 
   {
      int[][] num = {
              {45, 48, 65},
              {1, 5, 3, 9}, 
              {2, 6}, 
      };
      // using for..each loop
      for(int[] a: num)
      {
         for(int number: a) 
         {
            System.out.println(number);
         }
      }
   }
}



Output:

45
48
65
1
5
3
9
2
6


Class name of two dimensional array

Class name of two dimensional java array can be obtained using getClass.getName() method on object.

class Demo 
{
public static void main(String[] args)
{
int[][] b = new int[2][3];
System.out.println(b.getClass().getName());
}
}




Output:

[[I


3d array java

Java 3d array can be visualized as two dimensional array and is regarded as one of the complex form of array.

Also read – how to get file last modified date in java

Declaring – Syntax:
datatype[ ][ ][ ] arrayname;

Different ways to declare 3d array java,

int[ ]  [ ][ ]b;

int[ ]  [ ]b[ ];

int [ ][ ]b[ ];

int [ ]b[ ][ ];

Example: int[ ][ ][ ] b; // preferred 3d array declaration

Declaration and creation:

int[ ][ ][ ] b = new int[2][4][3];

Initialize a 3d array java

arrayname[arrayindex][rowindex][columnindex] = value;

Example: a[0][0][0] = 10;

Declaration, creation and initialization:

int[ ][ ][ ] b = {{{1, 2},{3, 4, 5, 6},{7, 8, 9}}};


3d array java can be referred like this arrayname[a][b][c], where ‘a’ represents array number, ‘b’ represents row number and ‘c’ as column number.

Syntax
arrayname[arrayindex][rowindex][columnindex]

Example

Now let’s see how to print 3d array java using for-each loop,

public class Demo 
{
public static void main(String[] args)
{
int[][][] a = {
{
{26, 36, 17},
{80, 15, 27}
},
{
{18, 38, 29, 93},
{16},
{89, 63}
}
};
// using for-each loop
for(int[][] arr2: a)
{
for(int[] arr1: arr2)
{
for(int item: arr1)
{
System.out.println(item);
}
}
}
}
}




Output:

26
36
17
80
15
27
18
38
29
93
16
89
63


To represent 3d array java in tabular format is as below,

3d array java

Class name of three dimensional array

Like two dimensional java array, here’s the class name of three dimensional java array.

class Demo 
{
   public static void main(String[] args) 
   {
      int[][][] b = new int[2][3][4];
      System.out.println(b.getClass().getName());
   }
}


Output:

[[[I