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 arrays. Data in multidimensional array is stored in the form of rows and columns. Data is accessed using row column index (based on index).

Syntax

datatype arrayname[dimension1][dimension2]

dataType[][] arrayName;
dataType [][]arrayName;
dataType arrayName[][];
dataType []arrayName[];


Here, datatype - defines datatype of elements. For example int, float arrayName - defines name of array. For example employee, students

Example:
int marks[][] = new int[6][3]; // two dimensional array
instantiating multidimensional array

marks[0][0] = 91;
marks[0][1] = 92;
marks[0][2] = 83;
marks[1][0] = 94;
marks[1][1] = 95;
marks[1][2] = 86;
marks[2][0] = 97;
marks[2][1] = 88;
marks[2][2] = 99;

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

Example of multidimensional java array

public class MultidimensionalArrayDemo 
{
public static void main(String[] args)
{
int marks[][] = {{91,82,93},{92,94,85},{94,94,95}};
// printing array
for(int a = 0; a < 3; a++)
{
for(int b = 0; b < 3; b++)
{
System.out.print(marks[a][b] + " ");
}
System.out.println();
}
}
}

Output:

91 82 93
92 94 85
94 94 95


Two dimensional array in java

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

Also read – treemap in java

In two dimensional array 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 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("marks[0][0] = " + arr[0][0]); 
   } 
} 

Output:

marks[0][0] = 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 array can be visualized as 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 marks[a][b][c], ‘a’ represents array number, ‘b’ represents row number and ‘c’ as column number.

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