Array dimension reflection
In this chapter you will learn:
Get array upperbound
public class Main {
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);
}/*from j av a2s.c om*/
}
Output:
Get the number of dimensions
public class Main {
public static void main(String args[]) {
String[][] data = new String[3][4];
System.out.println(getDimension(data));
}/*j a v a 2 s . com*/
public static int getDimension(Object array) {
int dim = 0;
Class c = array.getClass();
while (c.isArray()) {
c = c.getComponentType();
dim++;
}
return (dim);
}
}
Output:
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Array