Get array upperboundTag(s): Language


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 );
  }
}

blog comments powered by Disqus