Error 404
Error 404

Reputation: 107

How to identify multiple level array object class type regardless of array level?

The 'doSomething(T[] obj)' method accepts any level of array type object, but unable to dynamically identify the object type regardless of array level.

Based on Test.java, The multiple level array of Integer and String objects are not executed correctly. It returns "doSomething(T[] obj) false" instead of "doSomething(T[] obj) true";

Test.java:

/*  
    Actual Output:
    Integer: doSomething(T obj) true
    Integer[]: doSomething(T[] obj) true
    Integer[][]: doSomething(T[] obj) false
    String: doSomething(T obj) true
    String[]: doSomething(T[] obj) true
    String[][]: doSomething(T[] obj) false

    Expected Output:
    Integer: doSomething(T obj) true
    Integer[]: doSomething(T[] obj) true
    Integer[][]: doSomething(T[] obj) true
    String: doSomething(T obj) true
    String[]: doSomething(T[] obj) true
    String[][]: doSomething(T[] obj) true
*/
public class Test {

    public static void main(String[] args) throws Exception {
        System.out.println("0: " + doSomething(new Integer(1)));
        System.out.println("1: " + doSomething(new Integer[]{}));
        System.out.println("2: " + doSomething(new Integer[][]{}));

        System.out.println("0: " + doSomething(new String("")));
        System.out.println("1: " + doSomething(new String[]{}));
        System.out.println("2: " + doSomething(new String[][]{}));
    }

    public static <T> String doSomething(T obj) {

        if (Number.class.isInstance(obj)) {
            /*
             do something
             */
            return "doSomething(T obj) true";
        } else if (String.class.isInstance(obj)) {
            /*
             do something
             */
            return "doSomething(T obj) true";
        }

        return "doSomething(T obj) false";
    }

    public static <T> String doSomething(T[] obj) {

        if (Number[].class.isInstance(obj)) {
            /*
             do something regardless of array deep
             */
            return "doSomething(T[] obj) true";
        } else if (String[].class.isInstance(obj)) {
            /*
             do something regardless of array deep
             */
            return "doSomething(T[] obj) true";
        }

        return "doSomething(T[] obj) false";
    }
}

I would like it to be done dynamically instead of hard coded as below.

        if (Number[].class.isInstance(obj)) {
            /*
             do something
             */
            return "doSomething(T[] obj) true";
        } else if (Number[][].class.isInstance(obj)) {
            /*
             do something
             */
            return "doSomething(T[] obj) true";
        } else if (Number[][][].class.isInstance(obj)) {
        ...

We have no idea in future about the array level.

Upvotes: 2

Views: 64

Answers (2)

Abdolrahman Farshgar
Abdolrahman Farshgar

Reputation: 339

add this to your code:

public static <T> String doSomething(T[][] obj) {
    if (Number[][].class.isInstance(obj)) {
        return "doSomething(T[][] obj) true";
    } else if (String[][].class.isInstance(obj)) {
        return "doSomething(T[][] obj) true";
    }
    return "doSomething(T[][] obj) false";
}

Upvotes: -1

Andremoniy
Andremoniy

Reputation: 34900

In both cases (#2) Integer[][] is not Number[] type (as String[][] is not String[]), so your program gives you correct result.

Why it so? Because actually Integer[][] type declares an array type which elements are of the type Integer[], but itself it isn't Integer[].

So, if you want to "catch" such things you have to add:

} else if (Integer[][].class.isInstance(obj)) {

and

} else if (String[][].class.isInstance(obj)) {

conditions, or just single } else if (Object[][].class.isInstance(obj)) {.

UPDATE: As you wrote in your comment, you want to be able to work with arrays of arbitrary dimension. In this case such idea can help you:

    } else if (Object[][].class.isInstance(obj)) {
        for (T[] subArray : (T[][])obj) {
            doSomething(subArray);
        }
        /*
         do something regardless of array deep
         */
        return "doSomething(T[] obj) true";
    }

Upvotes: 3

Related Questions