Multidimensional array in java

In this post we are going to learn multidimensional array in java.

Multidimensional array in java

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

Syntax

datatype arrayname[dimension1][dimension2] = new datatype[rowsize][columnsize];

Here, datatype - defines datatype of elements. For example int, float arrayName - defines name of array. For example employee, students int marks[][] = new int[6][3]; // two dimensional array int marks[][][] = new int[2][3][4]; // three dimensional array

Size of multidimensional array can be determined by multiplying rowsize and columnsize, like this,

int marks[][] = new int[6][3]; // stores 18 elements i.e, 6 X 3 = 18
int marks[][][] = new int[2][3][4]; // stores 24 elements i.e, 2 X 3 X 4 = 24


Two dimensional array in java

To declare two dimensional array “new” operator is used. Here’s how to declare two dimensional array in java,

// declaring two dimensional array
datatype arrayName[][] = new datatype[rowsize][columnsize];
Example: int marks[][] = new int[6][3];

Here’s how to initialize a two dimensional array,

arrayName[rowindex][columnindex] = value;
Example: marks[0][0] = 98;

class TwoDimensionalDemo
{
   public static void main(String[] args)
   { 
      int marks[][] = new int[6][3]; 
      marks[0][0] = 98; 
      System.out.println("Row 0 column 0 value = " + arr[0][0]); 
   } 
} 

Output:

Row 0 column 0 value = 98


Let’s see another way to declare two dimensional array,

Syntax
datatype arrayname[][] = {                            {row0column0, row0column1},                             {row1column0,row1column1,}                           }; Example: int marks[][] = {{98, 92}, {93, 94}};
class TwoDimensionalDemo
{
   public static void main(String[] args)
   {
      int marks[][] = {{98, 92}, {93, 94}};
      for(int a = 0; a < 2; a++)
      {
         for(int b = 0; b < 2; b++)
         {
            System.out.println("marks[" + a + "][" + b + "] = " + marks[a][b]);
         }
      }
   }
}

Output:

marks[0][0] = 98
marks[0][1] = 92
marks[1][0] = 93
marks[1][1] = 94


Now let’s see how to access elements and represent a two dimensional array.

Also read – arrays in java

To access two dimensional array we use row index and column index. “marks[a][b]” is the way to refer two dimensional array where “a” and “b” refer to row index and column index.

Syntax
arrayname[rowindex][columnindex]

Example:
int marks[][] = new int[6][3];
marks[0][0] = 98;

class TwoDimensionalDemo
{
public static void main(String[] args)
{
int marks = {{98, 92}, {93, 94}};
System.out.println("marks[0][0] = " + arr[0][0]);
}
}

Output:

marks[0][0] = 98


We can represent two dimensional array in tabular format like this,

multidimensional array in java

So now to print all the elements of two dimensional array nested for loop is used. Outer “for loop” loops rows and inner “for loop” loops column.

class TwoDimensionalDemo
{
public static void main(String[] args)
{
int marks[][] = {{98, 92}, {93, 94}};
for(int a = 0; a < 2; a++)
{
for(int b = 0; b < 2; b++)
{
System.out.print(arr[a][b] + " ");
}
System.out.println();
}
}
}

Output:

98  92
93  94


Three dimensional array(3 D)

Three dimensional consists of two dimensional array and is regarded as one of the complex form of array.

Syntax:
datatype arrayname[][][] = new datatype[a][b][c];
Example: int marks[][][] = new int[2][3][4];

Now let’s learn how to initialize a three dimensional array,

arrayname[arrayindex][rowindex][columnindex] = value;
Example: marks[0][0][0] = 98;

class ThreeDimensionalDemo
{
public static void main(String[] args)
{
int marks[][][] = new int[2][3][4];
marks[0][0][0] = 98;
System.out.println("marks[0][0][0] = " + marks[0][0][0]);
}
}

Output:

marks[0][0][0] = 98


Let’s see another way to declare three dimensional array,

datatype arrayname[][][] = {
{
{valueA1, valueA1},
{valueA1, valueA1}
},
{
{valueA2, valueA2},
{valueA2, valueA2}
}
};

Example: int marks[][][] = {{{91, 92}, {93, 94}}, {{95, 96}, {97, 98}}};

class ThreeDimensionalDemo
{
public static void main(String[] args)
{
int marks[][][] = {{{91, 92}, {93, 94}}, {{95, 96}, {97, 98}}};
for(int a = 0; a < 2; a++)
{
for(int b = 0; b < 2; b++)
{
for(int c = 0; c < 2; c++)
{
System.out.println("marks[" + a + "][" + b + "][" + c + "] = " + marks[a][b][c]);
}
}
}
}
}

Output:

marks[0][0][0] = 91
marks[0][0][1] = 92
marks[0][1][0] = 93
marks[0][1][1] = 94
marks[1][0][0] = 95
marks[1][0][1] = 96
marks[1][1][0] = 97
marks[1][1][1] = 98


Three dimensional array can be referred like this,

Syntax
arrayname[arrayindex][rowindex][columnindex]

Example:
int marks[][][] = new int[2][3][4];
marks[][][] = 91;

class ThreeDimensionalDemo
{
public static void main(String[] args)
{
int marks[][][] = {{{91, 92}, {93, 94}}, {{95, 96}, {97, 98}}};
System.out.println("marks[0][0][0] = " + marks[0][0][0]);
}
}

Output:

marks[0][0][0] = 91


To represent three dimensional array in tabular format is as below,

multidimensional array in java

Now let’s see how to print three dimensional array using nested for loops. To print we need three for loops like this,

public class ThreeDimensionalDemo 
{
public static void main(String[] args)
{
int marks[][][] = {{{91, 92}, {93, 94}},
{{95, 96}, {97, 98}}};
for(int a = 0; a < 2; a++)
{
for(int b = 0; b < 2; b++)
{
for(int c = 0; c < 2; c++)
{
System.out.print(marks[a][b][c] + " ");
}
System.out.println();
}
System.out.println();
}
}
}

Output:

91    92
93    94

95    96
97    98