Non-rectangular double dimension array

Double dimension arrays are usually perceived and visualized as a grid or matrix of rows and columns. When the word matrix is used visualization tends to be square and when the word grid is used visualization tends to be rectangular due to the mathematical meaning of these two words! The reality is that double dimension just means or indicate that there are two dimensions viz- rows and columns. In fact, double dimension array as a square or rectangular array is just a special case of a double dimension array. So all of the following arrays are technically possible in Java:

Square double dimension array

Square double dimension array

rectangular double dimension array

Rectangular double dimension array

triangular double dimension array

Triangular double dimension array

irregular double dimension array

Irregular double dimension array

The square double dimension array is the simplest to understand and create. It can be created by:

int [][]arr = new int [ 5 ][ 5];

In the above case the double dimension array would be of five rows and five columns and arr.length would give the number of rows which is incidentally also equal to the number of columns.
The rectangular double dimension array can be created by:

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

In the above case the double dimension array would be of four rows and five columns and arr.length would give the number of rows and arr[0].length would give the number of columns in the 0th row which would be equal for all rows in the array because it is rectangular.

Creation of a triangular array as shown in the third figure above is slightly more involved and can be created as follows:

class TriangularArray{
    public static void display2DArray( int [][]arr ){
        for( int row = 0; row < arr.length ; row++ ){
            for( int col = 0; col < arr[ row ].length ; col++ ){
                System.out.print( arr[ row ][ col ] );
                if( col < arr[ row ].length - 1 ){
                    System.out.print("\t");
                }
            }
            System.out.println( );
        }
    }
    public static void main( String []args ){
        int a[][] = new int[ 5 ][];
        for( int row=0; row < a.length ; row++ ){
            a[ row ] = new int[ row + 1 ];
        } 
        display2DArray(a);      
    }
}

In the above program let us first focus on the main() function. We are creating a double dimension array by int a[][] and we are only initializing the row part that is there would be five rows. Now for each row from 0 to 4, we are initializing another array of size row+1, that is 0th row would be initialized with an array of size 1, 1st row would be initialized with an array of size 2 and so on. Now, let us focus on the function display2DArray, here the outer loop is running one upto one less that the number of rows and the inner loop is running upto one less than the number of columns in the respective row. Hence, this function display2DArray is suitable for any type of double dimension array because the loop would run for the respective number of columns in the array.

The most important thing to understand in the above example is that, technically, the double dimension array is not actually a grid but a set of arrays of reference variables referring to individual arrays. The correct representation for a triangular array which would hold for any two dimension array as well,  is as follows:

Triangular Array References

Triangular Array References

In the above diagram, the array a is a reference, referring to another array of refrence and this array of references actually point to the respective array. Note that this hold true for all two dimension arrays, irrespective of whether it’s square, rectanngular, triangular or an irregular array.
The above code to generate a triangular array or a non-rectangular double dimension array can easily be modified to create an arbitrary size irregular array by modifying line number 16, that is a[ row ] = new int[ row + 1 ]; to use the Random class as follows:

import java.util.*;
class ArbitraryArray{
    public static void display2DArray( int [][]arr ){
        for( int row = 0; row < arr.length ; row++ ){
            for( int col = 0; col < arr[ row ].length ; col++ ){
                System.out.print( arr[ row ][ col ] );
                if( col < arr[ row ].length - 1 ){
                    System.out.print("\t");
                }
            }
            System.out.println( );
        }
    }
    public static int getRandomIntegerInRange(int min, int max){
        Random objRandom = new Random();
        return min + (objRandom.nextInt( max-min+1 ) );
    }
    public static void main( String []args ){
        int a[][] = new int[ 5 ][];
        for( int row=0; row < a.length ; row++ ){
            a[ row ] = new int[ getRandomIntegerInRange( 1, 10 ) ];
        }

        display2DArray(a);      
    }
}

For details of random number generation in a range please refer to my earlier post “Generating random integers in a range“. The above programs would display zeros for all elements because I haven’t initialized the array so as to reduce clutter. Students are expected to write the relevant code to initialize the array on their own. In case there is any difficulty feel free to use the comment facility for help.

Follow me

Vinay Singh

Computer Teacher at Saint John's Academy
I am a GNU/Linux enthusiast and a computer science teacher by profession based at Allahabad, India. My hobbies include reading (mostly computer related stuff) and photography.
Follow me

Latest posts by Vinay Singh (see all)

One thought on “Non-rectangular double dimension array

  1. Pingback: Pascal triangle | www.VinaySingh.info

Leave a Reply