Java arrays are one instance long, and not all arrays in the same dimension must have equal lengths. However, you can get the length of the instances in.
Sizes can be counted by the number '[' in their name, this is faster than descending the type hierarchy. The following code:
int[][][] ary = {{{0},{1}}}; Class cls = ary.getClass(); boolean isAry = cls.isArray(); String clsName = cls.getName(); System.out.println("is array=" + isAry); System.out.println("name=" + clsName); int nrDims = 1 + clsName.lastIndexOf('['); System.out.println("nrDims=" + nrDims); Object orly = ary; for (int n = 0; n < nrDims; n++) { int len = Array.getLength(orly); System.out.println("dim[" + n + "]=" + len); if (0 < len) { orly = Array.get(orly, 0); } }
gives the following result:
is array=true name=[[[I nrDims=3 dim[0]=1 dim[1]=2 dim[2]=1
rsp
source share