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.

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.

In 2d array 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 in java.
int [ ][ ]b;
int[ ]  [ ]b;
int b[ ][ ];
int[ ] b[ ];

Here’s how to declare and create 2d array in 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 2d array in 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 SimpleTwodimensionalArray
{
   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 in java: ");    
      for(int a = 0; a < 3; a++) 
      {
         for(int b = 0; b < 2; b++) 
         {
            System.out.println(arrnumbers[a][b]);
         }
      }
   }
}


Output:

Two dimensional array in java:
2
4
6
8
10
12


two dimensional array java for loop

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


2d array length

java 2d 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 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


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


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.

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

Java 3d array 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 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 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 3d array in 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.


how to pass 2d array in java

To pass 2d array in java to method, reference of the array is passed to method.

Let’s see an example on how to pass 2d array in java.

import java.util.Scanner;
public class Pass2dArray
{
   public static void main(String[] args)
   {
      int[][] arrNumbers = passArray();
      // printing total of all elements
      System.out.println("Sum of all numbers: " + total(arrNumbers));
   }
   public static int[][] passArray()
   {
      Scanner sc = new Scanner(System.in);
      int[][] arrNumbers = new int[2][3];
      System.out.println("Please enter values in " + arrNumbers.length + " rows and " + arrNumbers[0].length + " columns: ");
      for(int a = 0; a < arrNumbers.length; a++)
      {
         for(int b = 0; b < arrNumbers[a].length; b++)
         {
            arrNumbers[a][b] = sc.nextInt();
         }
      }
      return arrNumbers;
   }
   public static int total(int[][] m)
   {
      int sum = 0;
      for(int row = 0; row < m.length; row++)
      {
         for(int column = 0; column < m[row].length; column++)
         {
            sum += m[row][column];
         }
      }
      return sum;
   }
}


Output:

Please enter values in 2 rows and 3 columns:

1 2 3
4 5 6
Sum of all numbers: 21