Thursday, October 16, 2025
HomeLanguagesJavaDetermine the Upper Bound of a Two Dimensional Array in Java

Determine the Upper Bound of a Two Dimensional Array in Java

Multidimensional arrays in Java are common and can be termed as an array of arrays. Data in a two-dimensional array in Java is stored in 2D tabular form. A two-dimensional array is the simplest form of a multidimensional array. A two-dimensional array can be seen as an array of the one-dimensional array for easier understanding. A two-dimensional array is declared with two dimensions that are also referred to as their bounds. 
 

Syntax of an Array of Arrays:

data_type[][] array_name = new data_type[x][y];

For example: int[][] arr = new int[20][30];    // 20 X 30 array of arrays

Direct Declaration of an Array of Arrays:

data_type[][] array_name = {
{valueR1C1, valueR1C2, ....},
{valueR2C1, valueR2C2, ....}
};

For example: int[][] arr = {{1, 2}, {3, 4}};

Allocation of values in an Array of Arrays

Java




// Allocation of values in an Array of Arrays
class GFG {
    public static void main(String[] args)
    {
  
        int[][] arr = new int[20][30];
        arr[0][0] = 100;
  
        System.out.println("arr[0][0] = " + arr[0][0]);
    }
}


Output

arr[0][0] = 100

 

Printing complete Array of Arrays

Java




// Printing complete Array of Arrays
class GFG {
    public static void main(String[] args)
    {
  
        int[][] arr = { { 1, 2 }, { 3, 4 } };
  
        for (int i = 0; i < 2; i++)
            for (int j = 0; j < 2; j++)
                System.out.println("arr[" + i + "][" + j
                                   + "] = " + arr[i][j]);
    }
}


Output

arr[0][0] = 1
arr[0][1] = 2
arr[1][0] = 3
arr[1][1] = 4

An array of Arrays in java is actually an array of a one-dimensional array. Each row of a two-dimensional array has a length field that holds the number of columns. However, we can get both the row upper bound and the column dimensions using:

int row_bound = array_name.length;
int column_bound = array_name[0].length;

Determine the Dimension of an Array of Arrays

Java




// Determine the Dimension of an Array of Arrays
class GFG {
    public static void main(String args[])
    {
        int[][] arr = new int[10][20];
        int row_bound = arr.length;
        int column_bound = arr[0].length;
        System.out.println("Dimension 1: " + row_bound);
        System.out.println("Dimension 2: " + column_bound);
    }
}


Output

Dimension 1: 10
Dimension 2: 20

 

Note: It is important to understand that Java doesn’t really have two-dimensional arrays. Java arrays are inherently jagged. It has arrays of arrays. So there is no one upper bound/length of the column level.

Example:

Java




// row bound and column bounds
class GFG {
    public static void main(String args[])
    {
        int[][] arr = { { 1, 2 },
                        { 3 },
                        { 4 },
                        { 5, 6, 7, 8 },
                        { 9, 10, 11, 12, 13, 14, 15 } };
        int row_bound = arr.length;
        int column_bound1 = arr[0].length;
        int column_bound2 = arr[1].length;
        int column_bound5 = arr[4].length;
        System.out.println("Dimension 1: " + row_bound);
        System.out.println("Dimension 2: " + column_bound1);
        System.out.println("Dimension 3: " + column_bound2);
        System.out.println("Dimension 4: " + column_bound5);
    }
}


Output

Dimension 1: 5
Dimension 2: 2
Dimension 3: 1
Dimension 4: 7
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11953 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS