Multi-Dimensional Arrays

  1. Defining square multi-dimensional arrays:

    Example: defining an 4x3 array of integers:
        int[][] B = new int[4][3];
    
    

    Result:

  2. Conclusion: A multi-dimensional arrays in Java is an array of arrays:

    Furthermore: each inner array can have different size (does not have to be "rectangular" in shape)

  3. Example: define the following 2-dimensional array:

  4. First: Defining a 2-dimensional "array object" reference variables:

    
                     int[][] B;       
    
    Alternate form:
                     int B[][];
    
    

    Result of this definition:

  5. Next: Defining an array of 3 (3 rows) int[]: variables:

    
             B = new int[3][];      
    
    

  6. Finally: Defining 3 (different length) int arrays and assign the address of these arrays to the 3 int[]: variables:

    
             B[0] = new int[3];       
             B[1] = new int[2];
             B[2] = new int[4];
    
    

    NOTE:

  7. DEMO program: click here