4

Given some object o, I need to find its dimensionality (eg: for int[x][y][z] the dimensionality is 3), I figured that any appropriate method would be in the class of the object.

int dimensionality = o.getClass().getName().indexOf('L');

works, but its source refers to a native method, so I'm left getting the answer from a string, rather than directly.

If anyone knows a better way of doing this it would be appreciated (although more for the sake of curiosity than necessity).

8
  • 4
    How many dimensions in {{{},{}},{}}? Commented Jan 1, 2014 at 0:59
  • The dimensionality of {{{},{}},{}} is either two or undefined. Commented Jan 1, 2014 at 1:06
  • If you're asking why I want the dimensionality of the object, I'm dealing with arrays of float data in either two or three dimensions; if you're asking why I'm calling indexOf('L') on the name of the class, the names of array classes begin with "[[[L" where the number of rectangular opening brackets is the dimensionality of the array class. Commented Jan 1, 2014 at 1:14
  • 1
    How can you calculate something that you can't even unambiguously define? Commented Jan 1, 2014 at 1:17
  • 1
    Component type of any so called multidimensional array is just another array. There's nothing here to argue about. Commented Jan 1, 2014 at 1:42

2 Answers 2

9

Here's a recursive solution using Class.isArray:

static int getDimensionality(final Class<?> type) {
    if (type.isArray()) {
        return 1 + getDimensionality(type.getComponentType());
    }
    return 0;
}

public static void main (String[] args) {
    System.out.println(getDimensionality(int.class));       // 0
    System.out.println(getDimensionality(int[].class));     // 1
    System.out.println(getDimensionality(int[][].class));   // 2
    System.out.println(getDimensionality(int[][][].class)); // 3
}

Although as PM 77-1 points out, this is not truly a measure of dimensionality but of the depth of a jagged array in terms of its static type. There are no true multidimensional arrays in Java, just arrays of arrays.

Sign up to request clarification or add additional context in comments.

1 Comment

In most scenarios you'll be dealing with arrays of a defined type, rather than Objects. Irrespective of java's use of true multidimensional arrays, dimensionality is defined in most use cases (eg: T[][][]...[] has a unique dimensionality value, although Object[][][]...[] may not).
0

If the array is more complex e.g.

Object[] oa = new Object[] {1, new Object[] {"a", new int[] {5, 6, 7}, "c"}, 3};

getComponentType is not gonna work. In that case you could determine the maximum nesting depth like this

static int maxDim(Object[] oa) {
    int maxDim = 0;
    for(Object o: oa) {
        if(o instanceof int[] || o instanceof long[] || o instanceof short[] ||
           o instanceof byte[] || o instanceof float[] ||
           o instanceof double[] || o instanceof boolean[]) {
            maxDim = Math.max(maxDim, 1);
        } else if(o instanceof Object[]) {
            maxDim = Math.max(maxDim, maxDim((Object[])o));
        }
    }
    return maxDim + 1;
}

1 Comment

The dimensionality of the given example is 1, since oa[1] is of type Object, not int[]. Even though oa[1] refers to an int[], one cannot subscript the inner array without typecasting oa[1] to int[] first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.