Tutorial: Multi Dimensional Array in Java

code for 2 dimensional Array in Java

Java, like most other powerful programming languages, supports multi-dimensional arrays, like 2 dimensional and 3 dimensional arrays. Previously we have discussed about working with one dimensional arrays in Java. In this tutorial let’s discuss about 2 dimensional and 3 dimensional arrays. The same principles are applied to higher dimension arrays as well.

2 Dimensional Array in Java

There are two ways to implement 2-dimensional arrays. Many languages reserve a block of memory large enough to hold all elements of the full, rectangular, array (number of rows times number of columns times the element size). Java doesn’t do this. Instead Java builds multi-dimensional arrays from many one-dimensional arrays, the so-called “arrays of arrays” approach.

Two-dimensional arrays are defined as “an array of arrays”. Since an array type is a first-class Java type, we can have an array of ints, an array of Strings, or an array of Objects. For example, an array of ints will have the type int[]. Similarly we can have int[][], which represents an “array of arrays of  ints”. Such an array is said to be a two-dimensional array.

The following command:

int[][] twodimArray = new int[3][4];

declares a variable, twodimArray, of type int[][], and it initializes that variable to refer to a newly created object. That object is an array of arrays of ints. Here, the notation int[3][4] indicates that there are 3 arrays of ints in the array twodimArray, and that there are 4 ints in each of those arrays.

There are a couple of interesting consequences of this: Rows may be different sizes. Also, each row is an object (an array) that can be used independently.

Declaration

int[][] a1; // Declares, but doesn't allocate, 2-dim array.

Allocation

As with all arrays, the new keyword must be used to allocate memory for an array. For example,

int[][] a1 = new int[3][4];

Or:

int[][] a1;
a1 = new int[3][4];

Or:

double[][] ID3 = {  {1.0, 0.0, 0.0},  {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}};

This allocates an int array with 3 rows and 4 columns. As with all objects, the values are initialized to zero (unlike local variables which are uninitialized).

This actually allocates 5 objects: a one-dimensional array of 4 elements for each of the rows, and a one-dimensional array of 3 elements, with each element pointing to the appropriate row array.

Processing

Often 2-dimensional arrays are processed with nested for loops. Notice in the following example how the rows are handled as separate objects.

Java Program

public class TwoDimArrayDemo{
  public static void main(String[] args) {
    int[][] twoDimArray = new int[3][4];
    for (int i=0; i<twoDimArray.length; i++) {
      for (int j=0; j<twoDimArray[i].length; j++) {
        twoDimArray[i][j] = i;
        System.out.print(" " + twoDimArray[i][j]);
      }
      System.out.println("");
    }
  }
}

Output

code for 2 dimensional Array in Java

3 Dimensional Array in Java

Java Program

class Array3D{
  public static void main(String args[]){
    int[][][] a2 = new int[2][2][4];
    for (int i = 0; i < a2.length; i++)
    for (int j = 0; j < a2[i].length; j++)
    for (int k = 0; k < a2[i][j].length; k++)
    System.out.println("a2[" + i + "][" + j + "][" + k + "] = "+ a2[i][j][k]);
  }
}

Output

code for 3 dimensional Array in Java

Explanation of Code & Output

Multi-dimensional array is processed through nested loops. For n-dimensional array you need n number of nested loops. As I said default value for each position of an array is ‘0’. It’s proved from the output of 3-dimensional array.

Checkout more useful tutorials and definitive guidelines on Java programming here.

About the author

Nitin Agarwal

A blogger, tech evangelist, YouTube creator, books lover, traveler, thinker, and believer of minimalist lifestyle.

1 Comment

Click here to post a comment