Array dimension reflection

In this chapter you will learn:

  1. Get array upperbound
  2. Get the number of dimensions

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:

  1. How to clone an Array
  2. Clones two dimensional float array
Home » Java Tutorial » Array
Java Array
Create an Array
Array Index and length
Multidimensional Arrays
Array examples
Array copy
Array compare
Array Binary search
Array Search
Array sort
Array to List
Convert array to Set
Array fill value
Array to String
Array element reverse
Array element delete
Array shuffle
Array element append
Array min / max value
Sub array search
Get Sub array
Array dimension reflection
Array clone