Multidimensional Arrays

In this chapter you will learn:

  1. How is the multidimensional arrays stored
  2. How to create a three-dimensional array
  3. What are Jagged array
  4. How to initialize multidimensional arrays during declaration

Multidimensional Arrays and its form

In Java, multidimensional arrays are actually arrays of arrays. For example, the following declares a two-dimensional array variable called twoD.

int twoD[][] = new int[4][5];

This allocates a 4-by-5 array and assigns it to twoD. This array will look like the one shown in the following:

The wrong way to think about multi-dimension arrays

right way to think about multi-dimension arrays

An irregular multi-dimension array

The following code use nested for loop to assign values to a two-dimensional array.

public class Main {
  public static void main(String args[]) {
    int twoD[][] = new int[4][5];
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 5; j++) {
        twoD[i][j] = i*j;// j  ava2s . c  om
      }
    }
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 5; j++) {
        System.out.print(twoD[i][j] + " ");
      }
      System.out.println();
    }
  }
}

This program generates the following output:

An example of a three-dimensional array

The following program creates a 3 by 4 by 5, three-dimensional array.

public class Main {
  public static void main(String args[]) {
    int threeD[][][] = new int[3][4][5];
// j  a va 2  s  . c  om
    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();
    }
  }
}

This program generates the following output:

Jagged array

When you allocate memory for a multidimensional array, you can allocate the remaining dimensions separately. For example, the following code allocates the second dimension manually.

public class Main {
  public static void main(String[] argv) {
    int twoD[][] = new int[4][];
    twoD[0] = new int[5];
    twoD[1] = new int[5];
    twoD[2] = new int[5];
    twoD[3] = new int[5];
  }//  j  a  v  a2 s. c  om
}

When allocating dimensions manually, you do not need to allocate the same number of elements for each dimension.

The following program creates a two-dimensional array in which the sizes of the second dimension are unequal.

public class Main {
  public static void main(String args[]) {
    int twoD[][] = new int[4][];
    twoD[0] = new int[1];
    twoD[1] = new int[2];
    twoD[2] = new int[3];
    twoD[3] = new int[4];
//from  j a  v a2s.com
    for (int i = 0; i < 4; i++){
      for (int j = 0; j < i + 1; j++) {
        twoD[i][j] = i + j;
      }
    }
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < i + 1; j++)
        System.out.print(twoD[i][j] + " ");
      System.out.println();
    }
  }
}

This program generates the following output:

The array created by this program looks like this:

Initialize multidimensional arrays

We can initialize multidimensional arrays during declaration by enclosing each dimension's initializer within its own set of curly braces.

public class Main{ 
  public static void main(String args[]) { 
    double m[][] = { 
            { 0, 1, 2, 3 }, // ja  v  a 2  s  .  c  om
            { 0, 1, 2, 3 }, 
            { 0, 1, 2, 3 }, 
            { 0, 1, 2, 3 } 
        }; 
    for(int i=0; i<4; i++) { 
      for(int j=0; j<4; j++){ 
        System.out.print(m[i][j] + " "); 
      }
      System.out.println(); 
    } 
  } 
}

When you run this program, you will get the following output:

Next chapter...

What you will learn in the next chapter:

  1. Calculate Average value of Array elements
  2. How to Create Fibonacci Series with array
  3. How to use two-dimensional double type array to do Matrix calculation
Home » Java Tutorial » Array
Java Array
Create an Array
Array Index and length
Multidimensional Arrays
Array examples
Array copy
Array compare
Array Binary search
Array Search
Array sort
Array to List
Convert array to Set
Array fill value
Array to String
Array element reverse
Array element delete
Array shuffle
Array element append
Array min / max value
Sub array search
Get Sub array
Array dimension reflection
Array clone