0

I want to write method which will check array size and if its greater then SIZE I want to throw error, however i don't know dimensions of array.

For example:

private void checkValidSize(int[] arr){
    if (arr.length > SIZE)
        throw new RuntimeException(Messages.WRONG_INPUT_ERROR);

}

If I call it with 1d array it works fine but it doesn't works for 2d arrays. As I understand 2d array is array itself containing arrays so i just need method which takes any kind of array as argument and measures its length.

If i knew that all arrays implement some kind of interface like IMesurable I would just wait for IMesurable in checkValidSize.

Is there anything like that or any other option so my function will work on any kind of array?

amirani
  • 260
  • 3
  • 9

2 Answers2

0

There are essentially 2 ways.

Way #1 is to use overloads. Arrays are covariant, so any array that's not a one dimensional primitive array is assignable to an Object[].

// this method accepts e.g.
// String[]
// int[][]
// etc...
static void checkValidSize(Object[] arr) {...}

// now define an overload for every primitive
static void checkValidSize(char[] arr) {...}
static void checkValidSize(int[] arr) {...}
...

Way #2 is to use java.lang.reflect.Array.

static void checkValidSize(Object shouldBeArr) {
    if (java.lang.reflect.Array.getLength(shouldBeArr)) {
        ...
    }
}

The major downside to that is there is no compilation checking of what you pass to it. You can pass something to it that's not an array.

(Some people might say "reflection is slow!" but that depends entirely on what you're doing. getLength is specially recognized by HotSpot nowadays and it gets inlined.)

Radiodef
  • 37,180
  • 14
  • 90
  • 125
0

There is no implemented interface or method that can be used to check if the Array is an 2d Array. if you take a look at Arrays#deepEquals(), you get an insight of how java is handling nested Arrays. E.g. by a lot of checking and casting.

So assuming that you are looking for the total int count excluding the arrays itself (not the plain #length property), you'll need to perform a 'deepLengthCheck'. Something like this:

public static int getDeepLength(Object array)
{
    int count = 0;

    if(array instanceof int[])
        count += ((int[])array).length;

    if(array instanceof Object[])
        for(Object current : (Object[])array)
            count += getNestedCount(current);

    return count;
}

static int getNestedCount(Object obj)
{
    if (obj instanceof Object[] )
       return getDeepLength((Object[])obj);

    if (obj instanceof int[] )
        return ((int[])obj).length;

    return 1;
}

The instanceof Object[] check is needed for the nested arrays, since an array itself is considered an Object. (and therefore an array of IntArrays == an array of Objects). Note that this is defenitly not the most optimal solution, and there might be better ways of doing this.

Also if you use primitive datatypes other than Integers, you'll need to check for those as well. (the Arrays#deepEquals0() gives an overview of what to check for).

If you are interested in more information about Arrays, and how they are created at Runtime etc. this post gives an brief explanation about the details of the Arrays in java.

Hope this was usefull!

Community
  • 1
  • 1
n247s
  • 1,898
  • 1
  • 12
  • 30