Two-Dimensional Arrays Tutorial

A two-dimensional array is basically an array of single-dimensional arrays. Arrays are objects, so when you declare an array, you are creating a reference variable. There are a few different ways to declare an array reference variable:
(type) (reference variable) (two pairs of brackets [][]) ;
(type) (two pairs of brackets [][]) (reference variable) ;
(type) (pair of brackets []) (reference variable) (pair of brackets []) ;
int numberArray[][];
int[][] numberArray;
int[] numberArray[];

After we declare the reference variable, we then assign it to an array object of specified length. This is how it is done:
(reference variable) (=) (new) (type) (pair of brackets [how many single-dimensional arrays] [single-dimensional array length]) ;
numberArray = new int[2][5]; // this will contain two 5 element arrays

We can do it all in one statement like this:
int numberArray[][] = new int[2][5];

Once we have created our array object, we can then initialize its elements. We can directly initialize each element like this:
// initialize the first single-dimensional array at index 0
numberArray[0][0] = 10;
numberArray[0][1] = 20;
numberArray[0][2] = 30;
numberArray[0][3] = 40;
numberArray[0][4] = 50;
// initialize the second single-dimensional array at index 1
numberArray[1][0] = 15;
numberArray[1][1] = 25;
numberArray[1][2] = 35;
numberArray[1][3] = 45;
numberArray[1][4] = 55;

There is also shorthand syntax that allows us to declare and initialize an array object without using the new keyword. The values for the elements are enclosed in curly braces and separated with commas; the number of pairs of braces and the length of the array is determined by the number of values inside the braces.
int numberArray[][] = { { 10, 20, 30 ,40, 50 }, { 15, 25, 35, 45, 55 } }; // two groups of single-dimension arrays each with a length of 5 elements

It is also very common to initialize a multi-dimensional array using nested for statements. I like to use x and y for the variable names because it allows me to visualize a row/column being built on an x/y axis.
int i = 10;
for (int x = 0; x < 2; x++) {
      for (int y = 0; y < 5; y++ ) {
            numberArray[x][y] = i;
            i += 10;
      }
      i = 15;
}

Reading Multi-dimensional Arrays

It is also very common to read a multi-dimensional array using nested for statements.
for (int x = 0; x < 2; x++) {
      for (int y = 0; y < 5; y++ ) {
            System.out.println(numberArray[x][y]);
      }
}

A better method is to read a multi-dimensional array using nested enhanced for statements.
for (int[] x : number) {
      for (int y : x) {
            System.out.println(y);
      }
}



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md TwoArray
C:\Java>cd TwoArray
C:\Java\TwoArray>Notepad TwoArray.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class TwoArray {
    public static void main(String args[]) {
        int arrayOne[][];
        arrayOne = new int[2][5];

        arrayOne[0][0] = 10;
        arrayOne[0][1] = 20;
        arrayOne[0][2] = 30;
        arrayOne[0][3] = 40;
        arrayOne[0][4] = 50;
        // initialize the second single-dimensional array at index 1
        arrayOne[1][0] = 15;
        arrayOne[1][1] = 25;
        arrayOne[1][2] = 35;
        arrayOne[1][3] = 45;
        arrayOne[1][4] = 55;

        int[][] arrayTwo = { { 10, 20, 30 ,40, 50 }, { 15, 25, 35, 45, 55 } };

        int[] arrayThree[]  = new int[2][5];
        int i = 10;
        for (int x = 0; x < 2; x++) {
            for (int y = 0; y < 5; y++ ) {
                arrayThree[x][y] = i;
                i += 10;
            }
            i = 15;
        } 

        // Read the arrays now
        System.out.println("Reading arrayOne...");
        for (int x = 0; x < 2; x++) {
            System.out.print("Element " + x + ": ");
            for (int y = 0; y < 5; y++ ) {
                System.out.print(arrayOne[x][y] + " ");
            }
            System.out.println();
        } 

        System.out.println("\nReading arrayTwo...");
        // we don't need to know the array length to read an array using
        for (int[] x : arrayTwo) {
            for (int y : x) {
                System.out.print(y + " ");
            }
            System.out.println();
        } 

        System.out.println("\nReading arrayThree...");
        for (int[] x : arrayThree) {
            for (int y : x) {
                System.out.print(y + " ");
            }
            System.out.println();
        } 

    }
}

Now switch back to the command prompt (CMD) and type in javac TwoArray.java and press Enter.
Now type in java TwoArray and press Enter.


C:\Java\TwoArray>javac TwoArray.java
C:\Java\TwoArray>java TwoArray
Reading arrayOne...
Element 0: 10 20 30 40 50
Element 1: 15 25 35 45 55

Reading arrayTwo...
10 20 30 40 50
15 25 35 45 55

Reading arrayThree...
10 20 30 40 50
15 25 35 45 55


Final thoughts

Multi-dimensional arrays are quite common; I personally have created numerous Android word search apps that utilize multi-dimensional arrays extensively. If you struggled with understanding how the enhanced for statement works then you should review my tutorial on the Enhanced For Statement.


Tutorials