2

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.

4
  • String[][].class.isInstance(obj) for 2 deep Commented Feb 12, 2016 at 9:08
  • @Ferrybig, I would like to do it dynamically, because I have no idea about the array level in the future. Commented Feb 12, 2016 at 9:10
  • Could you post a real working example and a sample input / output? What exactly are you trying to do here? Commented Feb 12, 2016 at 9:19
  • @Tunak I try to create a function to export the array as String. The Arrays.deepToString does the good job for array, but I'm too greedy to expect more than that. As an example (new String[][]{new String[]{"00", "01"}, new String[]{"10", "11"}}), The Arrays.deepToString returns [[00, 01], [10, 11]], but I expect [["00", "01"], ["10", "11"]]. Commented Feb 12, 2016 at 9:33

2 Answers 2

3

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";
    }
Sign up to request clarification or add additional context in comments.

3 Comments

I would like to do it dynamically, because I have no idea about the array level in the future.
This won't work with an empty array I believe, like new Integer[0][][][].
new Integer[0][][][] is nonsense, it will never contains any data
-1

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";
}

2 Comments

I checked the code ... it works fine with the above code!
No, the problem is not in syntactical correctness. You didn't got the idea what does author want. He want be able to work with arbitrary dimension of arrays.

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.