Multidimensional Array in Java

What is an array?

  • An array of arrays is known as a Multidimensional Array.
  • A multidimensional array is an array of arrays having multiple rows and columns.
  • Widely used Multidimensional arrays are 2-D & 3-D arrays.

1-D Array:

  • An array made up of 1 dimension x.
  • Containing x no. of elements.

Syntax:

data_type[] array_name  = new data_type[x];

Example 1:

int[] arr = new int[3];

Explanation:

  • The above array allocates an array containing three columns.
  • The new keyword allocates a space of defined size inside the memory.

Example 2: 1-D Array

Java Code

public class Main
{
	public static void main(String[] args) {
		System.out.println("Hello World");
		
	int[] arr = new int[3];//declaration   
	arr[0]=10;//initialization  
	arr[1]=20;  
	arr[2]=30;  
	//printing array  
	System.out.println("One dimensional array elements are");    
	System.out.println(arr[0]);    
	System.out.println(arr[1]);    
	System.out.println(arr[2]);  
	}
}

Output:

One dimensional array elements are
10
20
30

Explanation:

  • The above program creates a 1-Dimensional array of length 3.
  • We are initializing the values in the above array and retrieving them through their indexes.

2-D Array:

  • A 2-D array is made up of  2 dimensions x,y.
  • Containing x*y no. of elements.

Syntax: 2-D Array

data_type[][] array_name = new data_type[x][y];

Example 3:

int[][] arr = new int[3][4]; // 2-D array 
                            //Can hold a maximum of 12 elements

Explanation:

  • The above code allocates a 3 by 4 array and assigns it to arr.
  • Internally a matrix is implemented as an array of arrays.
  • Conceptually it is shown as below.

A Conceptual View of a 3 by 4, 2-Dimensional Array.

Note:

  • When allocating memory for a multidimensional array, we can specify only the memory of the first dimension.
  • We can allocate the remaining dimensions separately
  • As Java uses 0 base indexing, indexing starts from 0 and not 1.

Example 4:

int[][]  twoD = new int[3][];

twoD[0] = new int[5];
twoD[1] = new int[5];
twoD[2] = new int[5];

Explanation:

  • Here, We created a twoD array and initialized it with  3 rows and an unknown no. of columns. 
  • Later, we defined our length of columns which is 5.
  • The above picture shows the ith, jth dimension of a specific box inside the array.

Example 5: 2-D Array

Java Code

class twoDArray {
  public static void main(String[] args) {
    int[][] arr = {{1,2,3},{4,5,6}};
     
    for (int i = 0; i < arr.length; i++) { // 2 rows
      for (int j = 0; j < arr[i].length; j++) { // 3 columns
        // Accessing array element at ith row & jth column
        System.out.print(arr[i][j]);
      }
      System.out.println();
    }
  }
}

Output:

123 // 1st row & 3 columns
456 // 2nd row & 3 columns

Explanation:

  • The above program creates a 2 by 3, two-dimensional array.
  • Here, we are printing the index values in the loop.

3-D Array:

  • A 3-D array is an array of 2-D arrays.
  • Contains 3 dimensions x,y,z
  • Containing   x*y*z  no. of elements.

Syntax- 3-D array:

data_type array_name[ ] [ ] [ ] = new data_type[x][y][z];
  • data_type: The type of elements that will be stored in the array.  
  • array_name: name of the array
  • new: keyword to create an object in Java.
  • x, y, z: holds the numeric values for the various dimensions, 3 dimensions in this case.

Example 6: 

int[][][] arr = new int[3][4][2]; // 3-D array
                                 // Can hold elements upto 24.

Example 7: 3-D Array

Java Code

public class ThreeD {
  public static void main(String[] args) {

    int[][][] threeD = new int[3][4][5];

    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 4; j++) {
        for (int k = 0; k < 5; k++) {

          threeD[i][j][k] = i * j * k;
        }
      }
    }

    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 4; j++) {
        for (int k = 0; k < 5; k++) {

          System.out.print(threeD[i][j][k] + " ");
        }
        System.out.println();
      }
      System.out.println();
    }

  }
}

Output:

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12

0 0 0 0 0
0 2 4 6 8
0 4 8 12 16
0 6 12 18 24

Explanation:

  • The above program creates a 3 by 4 by 5 array, three-dimensional array.
  • It then stores each element with the product of its indexes.

Special thanks to Abbas Hussain Muzammil for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article