Get array upperbound in Java Multidimensional Arrays



In order to get the array upperbound of a multidimensional array, we use the length() method. For a 2D array, the length() method returns the number of rows. We can access the number of columns using the array_name[0].length method.

Let us see a program to get the array upperbound in Java Multidimensional arrays

Example

 Live Demo

public class Example {
   public static void main(String args[]) {
      String[][] str = new String[5][10];
      System.out.println("1st dimension : " + str.length); // displays the number of rows
      System.out.println("2nd dimension : " + str[0].length); // displays the number of columns
   }
}

Output

1st dimension : 5
2nd dimension : 10

Advertisements