Java Data Type Tutorial - Java Multi Dimenaional Array








We can store a tabular data in a two dimensional array.

A multi- dimensional array is declared using a pair of brackets [] for each dimension in the array declaration.

For example, a two dimensional array of int is defined as shown:

int[][] table;

Here, table is a reference variable that can hold a reference to a two-dimensional array of int.

A two-dimensional array of int with three rows and two columns can be created as shown:

table = new int[3][2];

The indexes of each dimension in a multi-dimensional array are zero-based.

Each element of the table array can be accessed as table[rownumber][columnNumber].

The row number and the column number always starts at zero.

For example, the following code assigns a value to the first row and the second column in the table array as shown:

table[0][1] = 32;

You can assign a value 1 to the third row and the first column like so:

table[2][0] = 1;

We must specify the dimension of at least the first level array at the time we create a multi-dimensional array.

For example,

table = new int[3][];

This statement creates only first level of array.

Only table[0], table[1] and table[2] exist at this time.

They are referring to null. table.length has a value of 3

table[0], table[1] and table[2] are arrays of int, we can assign them values as

table[0] = new int[2]; // Create two  columns  for row 1 
table[1] = new int[2]; // Create two  columns  for row 2 
table[2] = new int[2]; // Create two  columns  for row 3

Java can create a two-dimensional array with different number of columns for each row. Such an array is called a ragged array.





Example

The following code shows how to create a ragged array.

public class Main {
  public static void main(String[] args) {
    // Create a two-dimensional array of 3 rows
    int[][] raggedArray = new int[3][];
/*  w ww .  j av  a 2 s. com*/
    // Add 2 columns to the first row
    raggedArray[0] = new int[2];

    // Add 1 column to the second row
    raggedArray[1] = new int[1];

    // Add 3 columns to the third row
    raggedArray[2] = new int[3];

    // Assign values to all elements of raggedArr
    raggedArray[0][0] = 1;
    raggedArray[0][1] = 2;
    raggedArray[1][0] = 3;
    raggedArray[2][0] = 4;
    raggedArray[2][1] = 5;
    raggedArray[2][2] = 6;

    // Print all elements. One row at one line
    System.out.println(raggedArray[0][0] + "\t" + raggedArray[0][1]);
    System.out.println(raggedArray[1][0]);
    System.out.println(raggedArray[2][0] + "\t" + raggedArray[2][1] + "\t"
        + raggedArray[2][2]);
  }
}

The code above generates the following result.





Accessing Elements of a Multi-Dimensional Array

Typically, a multi-dimensional array is populated using nested for loops.

The number of for loops used to process a multi-dimensional array equals the number of dimensions in the array.

The following code shows how to access Elements of a Multi-dimensional Array

public class Main {
  public static void main(String[] args) {
    int[][] myArray = new int[3][];
    myArray[0] = new int[2];
    myArray[1] = new int[1];
    myArray[2] = new int[3];
/*from w  w  w .  j a va  2  s. co m*/
    // Populate the ragged array using for loops
    for (int i = 0; i < myArray.length; i++) {
      for (int j = 0; j < myArray[i].length; j++) {
        myArray[i][j] = i + j;
      }
    }

    // Print the array using for loops
    for (int i = 0; i < myArray.length; i++) {
      for (int j = 0; j < myArray[i].length; j++) {
        System.out.print(myArray[i][j] + "\t");
      }

      // Add a new line after each row is printed
      System.out.println();
    }
  }
}

The code above generates the following result.

Initializing Multi-Dimensional Arrays

We may initialize the elements of a multi-dimensional array by supplying the list of values at the time of its declaration or at the time of creation.

The number of initial values for each dimension will determine the length of each dimension in the array.

The list of values for a level is enclosed in braces.

For a two-dimensional array, the list of values for each row is enclosed in a pair of braces, like so:

int[][] arr = {{10, 20, 30},   
               {1, 2},   
               {2, 3, 4, 5}};

This statement creates a two-dimensional array with three rows.

The following code shows how to initialize a two-dimensional String array:

String[][] acronymlist = {{"A", "a"},
                          {"B", "b"},
                          {"C", "c"}};

We can initialize the elements of a multi-dimensional array at the time we create it.

int[][] arr = new int[][]{{1, 2},   {3,4,5}};