codescracker


java

Java Multidimensional Array



« Previous Tutorial Next Tutorial »


You can simply say multidimensional arrays as arrays of arrays. These, as you might expect, look and act like regular multidimensional arrays. Nevertheless, as you will see that there are a couple of subtle differences.

Java Two Dimensional Array

To declare a multidimensional array variable, determine each additional index using another set of square brackets. For instance, the below code fragment declares a two-dimensional array variable called twoD :

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

This allocates a 4 by 5 array and then assigns it to twoD. Actually, this matrix is made as an array of array of int.

Java Two Dimensional Array Example

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

/* Java Program Example - Java Multidimensional Arrays
 * This program demonstrates a two-dimensional (2D) array
 */

public class JavaProgram
{   
    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;
                k++;
            }
        }
        
        for(i=0; i<4; i++)
        {
            for(j=0; j<5; j++)
            {
                System.out.print(twoD[i][j] + " ");
            }
            System.out.println();
        }
        
    }
}

When the above Java program is compile and executed, it will produce the following output:

java two dimensional array

When you allocate the memory for a multidimensional array, then you only need to specify the memory for the first (leftmost) dimension. You can also allocate the remaining dimensions separately. For instance, the code given below allocates the 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];

Although there is no advantage in individually allocating the second dimension arrays in this situation, there may be in others. For instance, when you allocate dimensions manually, you do not need to allocate the same number of elements for each dimension. As told earlier, since multidimensional arrays are arrays of arrays, the length of the each array is under your control. For instance, the given program creates a two-dimensional array in which the sizes of the second dimension are unequal :

/* Java Program Example - Java Two-dimensional Arrays
 * Manually allocate differing size second dimensions
 */

public class JavaProgram
{   
    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;
                k++;
            }
        }
        
        for(i=0; i<4; i++)
        {
            for(j=0; j<i+1; j++)
            {
                System.out.print(twoD[i][j] + " ");
            }
            System.out.println();
        }
    }
}

When the above Java program is compile and executed, it will produce the following output:

java multidimensional arrays

In Java, it is possible to initialize multidimensional arrays. To do so, simply enclose each dimension's initializer in its own set of curly braces.

The following program creates a matrix where each element holds the product of row and column indexes. Notice here that you can also use expressions as well as literal values inside of array initializers.

/* Java Program Example - Java Multidimensional Arrays
 * Initialize a two-dimensional arrays.
 */

public class JavaProgram
{   
    public static void main(String args[])
    {
        
        double m[][] = {
            { 0*0, 1*0, 2*0, 3*0 },
            { 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();
        }
        
    }
}

When the above Java program is compile and executed, it will produce the following output:

multidimensional arrays in java

As you can see here that each row in the array is initialized as specified in the initialization lists.

Java Three Dimensional Array

Now, let's look this example that uses a multidimensional (three-dimensional) array. The following program creates a 3 by 4 by 5, three-dimensional array. This then loads each element with the product of its indexes. Finally, it displays these products.

Java Three Dimensional Array Example

Here is an example program, demonstrates the concept and use of three dimensional arrays in Java:

/* Java Program Example - Java Multidimensional Arrays
 * This program demonstrates a three-dimensional array. */

public class JavaProgram
{   
    public static void main(String args[])
    {
        
		/* initializing three-dimensional arrays */
        int threeD[][][] = new int[3][4][5];
        int i, j, k;
        
        for(i=0; i<3; i++)
        {
            for(j=0; j<4; j++)
            {
                for(k=0; k<5; k++)
                {
                    threeD[i][j][k] = i * j * k;
                }
            }
        }
        
		/* displaying three-dimensional arrays */
        for(i=0; i<3; i++)
        {
            for(j=0; j<4; j++)
            {
                for(k=0; k<5; k++)
                {
                    System.out.print(threeD[i][j][k] + "   ");
                }
                System.out.println();
            }
            System.out.println();
        }
        
    }
}

When the above Java program is compile and executed, it will produce the following output:

java three dimensional arrays

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test