-1

I have found answers to this question in c# but not in java. I want to make my own class to work with vectors and matrices, and perform various operations such as the dot product. Since an array will be passed on to the class, without knowing its dimensions beforehand, it would be important to know it. I was wondering if there is any function to get it. Thanks in advance. Here is an example:

int[] array = new int[10]; //this would be a 1D array of length 10

int[][][] array2 = new int[5][10][2]; //This one is 3D of length 100

What I want is "the number of square brackets" there are, which state the dimension of the array.

joroba3
  • 21
  • 1
  • 4
  • @BackSlash I am refering not to the length but to the dimensions(how many square brackets it has. [] would be one and [][][] would be 3). – joroba3 Dec 26 '16 at 11:10
  • Now, the question is: why do you need this? You'll have this information when coding, and even if you are passing the array to a method, you can't pass a multidimensional array to a method which accepts only monodimensional arrays and viceversa, so in the method you'll know this information for sure. What are you trying to accomplish? – BackSlash Dec 26 '16 at 11:23
  • To the close voters: This is **not** a duplicate of the question that asked about the size of a 2D array. This one is about the *dimensions* of an array which is only known as an object of type `Object`. – Marco13 Dec 26 '16 at 15:23
  • It is, however, a duplicate of http://stackoverflow.com/questions/23230854/getting-unknown-number-of-dimensions-from-a-multidimensional-array-in-java , as I noticed now... – Marco13 Dec 26 '16 at 15:46

2 Answers2

1

A simple solution would be to create a recursive method using Class#isArray and Class#getComponentType:

public class ArrayDimensions
{
    public static void main(String[] args)
    {
        Integer a = null;
        Integer a0 = 0;
        int a1[] = new int[0];
        int a2[][] = new int[0][0];
        int a3[][][] = new int[0][0][0];
        int a4[][][][] = new int[0][0][0][0];

        System.out.println(getDimensions(a)); // Prints -1
        System.out.println(getDimensions(a0)); // Prints 0
        System.out.println(getDimensions(a1)); // Prints 1
        System.out.println(getDimensions(a2)); // Prints 2
        System.out.println(getDimensions(a3)); // Prints 3
        System.out.println(getDimensions(a4)); // Prints 4
    }

    private static int getDimensions(Object array)
    {
        if (array == null)
        {
            return -1;
        }
        return getDimensions(array.getClass());
    }

    private static int getDimensions(Class<?> c)
    {
        if (c == null)
        {
            return 0;
        }
        if (!c.isArray())
        {
            return 0;
        }
        return 1 + getDimensions(c.getComponentType());
    }
}

A side note: Although there may be application cases for such a method, the intention to write a "matrix and vector library" based on raw, multidimensional arrays sounds a bit questionable (it is not necessarily "wrong", but a but dubious)

Marco13
  • 53,703
  • 9
  • 80
  • 159
0

I don't get the reason of your requirement, but you can do something like this:

int getBracketCount(Object array) {
    String className = array.getClass().getName();

    int bracketCount = className.length() - className.replace("[", "").length();
    return bracketCount;
}

The class name for arrays comes with N opening square brackets followed by the class name of the array, where N is the number of dimensions of the array.

So, for int[][][], class name will be something like [[[I. Three square brackets in the class name = three dimensions. For simple objects you shouldn't get square brackets in the class name.

Here is a demo: http://ideone.com/he8FDF

BackSlash
  • 21,927
  • 22
  • 96
  • 136