Multidimensional array in java

Let’s learn multidimensional array in java.

Multidimensional array in java

Multidimensional array is an array of arrays having multiple rows and columns. Data or elements in multidimensional array is stored in the form of rows and columns. Data is accessed using row column index.

Syntax:

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


Two dimensional array java or 2d arrays in java

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

In two dimensional array java data is stored in rows and columns. To access data or elements in two dimensional array we use row index and column index. Here’s how to declare two dimensional array java.

Syntax:
datatype[][] arrayName;

Declaring 2d arrays in java: Different ways to declare 2 dimensional array java.
int [ ][ ]b;
int[ ]  [ ]b;
int b[ ][ ];
int[ ] b[ ];

Here’s how to declare and create 2 dimensional array java.

int[][] n; // array declaration

int[][] n = new int[2][3]; // array creation

how to initialize 2d array in java?

n[0][0] = 1;

n[0][1] = 2;

n[0][2] = 3;

…. so on.

Also we can declare, create and initialize two dimensional array java in a single line like this,

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

multidimensional array in java

Let’s see two dimensional array in java example program.

public class TwodimensionalArrayExample
{
   public static void main(String[] args) 
   {
      // declare and initialize two dimensional array
      int[][] arrnumbers = {{2, 4},{6, 8},{10, 12}};   
      System.out.println("Two dimensional array: ");    
      for(int a = 0; a < 3; a++) 
      {
         for(int b = 0; b < 2; b++) 
         {
            System.out.println(arrnumbers[a][b]);
         }
      }
   }
}


Output:

Two dimensional array:
2
4
6
8
10
12


2d array length

Two dimensional array length is number of rows a 2D array has. The number of columns may vary in a 2D array but not the rows. Let’s see example on two dimensional array length.

public class TwoDimensionalArrayLength
{
   public static void main(String[] args) 
   {
      int[][] arrNumber = { 
              { 1, 3, 5 },
              { 7, 9, 2 },
              { 1, 2, 3, 4, 5 } };
      System.out.println("Java two dimensional array length: " + arrNumber.length);
   }
}


Output:

Java two dimensional array length is: 3


Let’s see another example java 2d array length.

public class TwoDimensionalArrayLength
{
   public static int[][] a = new int[6][3];
   // returns length of rows
   public static int lengthRow = a.length;
   // to get length of column specify row before length property
   public static int lengthColumn = a[0].length;
   public static void main(String[] args)
   {
      System.out.println(lengthRow);
      System.out.println(lengthColumn);
   }
}


Output:

6
3


Class name of two dimensional array

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

public class Demo
{
   public static void main(String[] args)
   {
      int[][] b = {{1,2,3},{4,5,6}};
      System.out.println(b);
   }
}


Output:

[[I@5305068a

As you can see the ouput, two braces ([) denote 2d array, “I” represent integer class and hashcode value with “@” symbol.


3d array in java or three dimensional array in java

3d array in java can be visualized as an array of two dimensional arrays and is regarded as one of the complex form of array.

Syntax:

datatype[][][] arrayname = new arrayname[][][];

Different ways to declare 3d array in java.

int[ ]  [ ][ ]b;

int[ ]  [ ]b[ ];

int  [ ][ ]b[ ];

int  [ ]b[ ][ ];

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

Creation:

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

Initialize a 3d array in java

a[0][0][0] = 10; // assign value 10 as first element of array ‘a’.

Declaration, creation and initialization:

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

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

Example

Now let’s see how to print 3 dimensional array java using loops as it retrieves all elements at once.

public class Java3dArray
{
   public static void main(String[] args)
   {
      int[][][] num = {{{1,2,3},{4,5,6},{7,8,9}}};
      for(int x = 0; x < num.length; x++)
      {
         for(int y = 0; y < num[x].length; y++)
         {
            for(int z = 0; z < num[x][y].length; z++)
            {
               System.out.print(num[x][y][z]);
            }
            System.out.println();
         }
      }
   }
}


Output:

1 2 3
4 5 6
7 8 9


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

multidimensional array in java

Class name of three dimensional array

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

public class Demo
{
   public static void main(String[] args)
   {
      int[][][] n = {{{1,2,3},{4,5,6},{7,8,9}}};
      System.out.println(n);
   }
}


Output:

[[[I@5305068a

As you can see the ouput three braces ([) denote 3d array, “I” represent integer class and hashcode value with “@” symbol.


Also read – operators in java