We use cookies to collect and analyze information on site performance and usage,
to provide social media features and to enhance and customize content and advertisements.
class ArrayLength {
public static void main(String args[]) {
String[][] data = new String[3][4];
System.out.println("Dimension 1: " + data.length);
System.out.println("Dimension 2: " + data[0].length);
}
}
To get the number of dimensions :
public class ArrayDim {
public static void main(String args[]) {
String[][] data = new String[3][4];
System.out.println("This array has " + ArrayDim.getDim(data) + " dimensions");
// expected output :
// "This array has 2 dimensions"
}
public static int getDim(Object array ) {
int dim=0;
Class c = array.getClass();
while( c.isArray() ) {
c = c.getComponentType();
dim++;
}
return( dim );
}
}