Java Array multidimensional Arrays

Introduction

Java multidimensional arrays are arrays of arrays.

To declare a multidimensional array variable, specify each index using another set of square brackets.

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.

Internally, this matrix is implemented as an array of arrays of int.

The following program numbers each element in the array from left to right, top to bottom, and then displays these values:

// Demonstrate a two-dimensional array.
public class Main {
  public static void main(String args[]) {
    int twoD[][]= new int[4][5];
    int i, j, k = 0;

    for(i=0; i<4; i++) 
      for(j=0; j<5; j++) {
        twoD[i][j] = k;//from  w  ww  .  ja  va 2 s . com
        k++;
      }

    for(i=0; i<4; i++) {
      for(j=0; j<5; j++)
        System.out.print(twoD[i][j] + " ");
      System.out.println();
    }
  }
}

When allocating memory for a multidimensional array, you need only specify the memory for the first dimension.

You can allocate the remaining dimensions separately.

For example, this following code allocates memory for the first dimension of twoD when it is declared.

It allocates the second dimension manually.

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]; 

You can allocates the second dimension with different length.

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

For example, the following program creates a two-dimensional array in which the sizes of the second dimension are unequal:

// Manually allocate differing size second dimensions.
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];

    int i, j, k = 0;

    for(i=0; i<4; i++) 
      for(j=0; j<i+1; j++) {
        twoD[i][j] = k;// ww  w .ja  va  2  s  . co m
        k++;
      }

    for(i=0; i<4; i++) {
      for(j=0; j<i+1; j++)
        System.out.print(twoD[i][j] + " ");
      System.out.println();
    }
  }
}

Initialize multidimensional arrays

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

The following program creates a matrix where each element contains the product of the row and column indexes.

// Initialize a two-dimensional array.
public class Main {
  public static void main(String args[]) {
    double m[][] = {
      { 0*0, 1*0, 2*0, 3*0 },/* w w w .j  a v  a 2s. c o m*/
      { 0*1, 1*1, 2*1, 3*1 },
      { 0*2, 1*2, 2*2, 3*2 },
      { 0*3, 1*3, 2*3, 3*3 }
    };
    int i, j;

    for(i=0; i<4; i++) {
      for(j=0; j<4; j++)
        System.out.print(m[i][j] + " ");
      System.out.println();
    }
  }
}

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

// Demonstrate a three-dimensional array.  
public class Main {  
  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();  //  w  w  w . j a  v a 2s.c o m
      }  
      System.out.println();  
    }  
  }  
} 



PreviousNext

Related