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.

Also read – one dimensional array in java

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 in java

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

Also read – two dimensional array in java

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];

how to initialize 2d array in java?

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

It can be visualized as 3 integer array of length 4.

Also we can declare, create and initialize 2d array in java in 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 simple two-dimensional array program in java,

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


Output:

Simple two-dimensional array:
2
4
6
8
10
12


how to fill a 2d array java

To fill a 2d array in java we are using two for loops to fill values with random numbers.

Then to print random numbers again we are using two for loops. Let’s see an example on how to fill a 2d array java,

public class Fill2dArrayExample
{
   public static void main(String[] args) 
   {
      int[][] arrNumbers = new int[3][3];
      for(int a = 0; a < 3; a++)
      {
         for(int b = 0; b < 3; b++)
         {
            arrNumbers[a][b] = (int) (Math.random() * 6) ;
         }
      }
      for(int x = 0; x < 3; x++)
      {
         for(int y = 0; y < 3; y++)
         {
            System.out.print(arrNumbers[x][y] + " ");
         }
         System.out.println();
      }
   }
}


Output:

2 2 3
2 1 5
0 3 3


two dimensional array length

Length of a two dimensional array in java is number of rows a 2D array has. Let’s see java two dimensional array length example,

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


Now let’s see two dimensional string array in java example,

public class TwoDimensionalStringArray 
{
   public static void main(String[] args) 
   {
      String[][] animals = {
              { "Lion", "Tiger", "Cheetah" },
              { "Deer", "Jackal", "Bear" },
              { "Hyena", "Fox", "Elephant" } };

      for(int a = 0; a < animals.length; a++) 
      {
         System.out.print(animals[a][0] + " ");
         for(int b = 1; b < animals[a].length; b++) 
         {
            System.out.print(animals[a][b] + " ");
         }
         System.out.println();
      }
   }
}


Output:

Lion Tiger Cheetah
Deer Jackal Bear
Hyena Fox Elephant


dynamic 2d array in java

Let’s learn how to create dynamic two dimensional array in java,

import java.util.ArrayList;
import java.util.List;

public class Dynamic2dArray 
{
   public static void main(String[] args) 
   {
      List<int[]> li = new ArrayList<>();
      li.add(new int[]{2,4,6});
      li.add(new int[]{3,5});
      li.add(new int[]{1});
      // element at row 0, column 0
      System.out.println("Element at [0][0]: " + li.get(0)[1]);
      // get element at row : 1, column : 1
      System.out.println("Element at [1][1]: " + li.get(1)[1]);
   }
}


Output:

Element at [0][0]: 4
Element at [1][1]: 5

Class name of two dimensional array

Class name of two dimensional array in java 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 in java

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

Defining 3d array in java:
datatype[ ][ ][ ] arrayname = new arrayname[][][];

Different ways to declare 3d array java,

int[ ]  [ ][ ]b;

int[ ]  [ ]b[ ];

int  [ ][ ]b[ ];

int  [ ]b[ ][ ];

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; // assign value 10 at 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.

Syntax
arrayname[arrayindex][rowindex][columnindex]

Example

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

public class Java3dArray
{
   public static void main(String[] args) 
   {
      int[][][] emp = {
              {
                  {2, 4, 6},
                  {8, 1, 3}
              },
              {
                  {5, 7, 9},
                  {2, 4, 6},
              }
      };

      for(int tables = 0; tables < 2; tables++)
      {
         for(int rows = 0; rows < 2; rows++)
         {
            for(int columns = 0; columns < 3; columns++)
            {
               System.out.print("emp[" +tables+ "][" +rows+ "][" +columns+ "] = " + emp[tables][rows][columns]+ "\t");
            }
            System.out.println();
         }
         System.out.println();
      }
   }
}


Output:

emp[0][0][0] = 2 emp[0][0][1] = 4 emp[0][0][2] = 6
emp[0][1][0] = 8 emp[0][1][1] = 1 emp[0][1][2] = 3

emp[1][0][0] = 5 emp[1][0][1] = 7 emp[1][0][2] = 9
emp[1][1][0] = 2 emp[1][1][1] = 4 emp[1][1][2] = 6


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.

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


Output:

[[[I