tags:

views:

144

answers:

6

Hi

While using method Arrays.deepToString(Object [] a) I am facing this problem which I can put down in this way.

 Object [] not_allowed = new  int[]{7, 9, 8};
 Object [] allowed  = new int[][]{{1, 2, 3}, {6, 5, 4}};

Why i am not allowed to assign int[] to Object[] in single dimension case but i am allowed to do such in multidimension case.

+5  A: 

ints are not Objects, that's why the first assignment is illegal: An array of ints is not an array of Objects.

But arrays, no matter of what, are objects. You're successfully assigning the outer data structure, which is an array of arrays, to an array of Objects.

Carl Smotricz
+5  A: 

Lower-case int is a primitive and does not inherit from Object. Try using Integer instead.

MattGrommes
+2  A: 

The first assignment does not work because the element type of int[] is int, not Object. Since (you are trying to assign to Object[], the element type should be an Object, not int)

The second assignment does work, because int[] is an Object, therefore int[][] is an Object[].

Roland Bouman
`int[]` is an `Object`, true, but it does not follow that `int[][]` is an `Object[]` because you can insert items into an `Object[]` that you cannot insert into an `int[][]`.
jk
Right, agree. Perhaps I should have written "int[][] is a particular case of Object[]" instead of saying that "int[][] is an Object[]"
Roland Bouman
+7  A: 

Primitive types like int are not Objects while an array is an Object-- you can assign any array to an Object reference

Object o = new  int[]{7, 9, 8};

new int[][] is an array of objects and thus can be assigned to Object[]. You may want to write a utility method like this to do what you want:

public static String arrayToString(Object o) {
    if(o instanceof Object[])  {
        return Arrays.deepToString((Object[]) o);
    }
    else if(o instanceof long[]) {
        return Arrays.toString((long[]) o);
    }
    else if(o instanceof int[]) {
        return Arrays.toString((int[]) o);
    }
    else if(o instanceof short[]) {
        return Arrays.toString((short[]) o);
    }
    else if(o instanceof byte[]) {
        return Arrays.toString((byte[]) o);
    }
    else if(o instanceof float[]) {
        return Arrays.toString((float[]) o);
    }
    else if(o instanceof double[]) {
        return Arrays.toString((double[]) o);
    }
    else if(o instanceof boolean[]) {
        return Arrays.toString((boolean[]) o);
    }
    throw new IllegalArgumentException("input is not an array");
}

Example:

Object intArray = new  int[]{7, 9, 8};
Object[] intintArray  = new int[][]{{1, 2, 3}, {6, 5, 4}};
Object[] intintintArray  = new int[][][]{{{1, 2, 3}, {6, 5, 4}},
               {{1, 2, 3}, {6, 5, 4}}};
System.out.println(arrayToString(intArray));
System.out.println(arrayToString(intintArray));
System.out.println(arrayToString(intintintArray));

Output:

[7, 9, 8]
[[1, 2, 3], [6, 5, 4]]
[[[1, 2, 3], [6, 5, 4]], [[1, 2, 3], [6, 5, 4]]]
Chandra Patni
+2  A: 

Your first line is invalid :

Object [] not_allowed = new  int[]{7, 9, 8};

This is because int is not an Object. This will be valid :

Object allowed = new  int[]{7, 9, 8};
fastcodejava
A: 

One additional point to mention is that the line that is allowed

Object [] allowed  = new int[][]{{1, 2, 3}, {6, 5, 4}};

actually shouldn't be either because you should be able to follow it up with

allowed[0] = "Surprise!";

and so manage to put a String into an array that should contain only int[]s. This is why Java has to check each assignment to an array element at run time to make sure that the assigned element is compatible with the real type of the array, and not just let compile-time type checking handle it. So the above code compiles but will cause an exception on trying to assign the String.

This problem also affects only arrays. When generics were added to Java, the designers knew better and in generic collections, a collection of int[] is not a subtype of a collection of Object, so if, in your example, you switch from arrays to generic ArrayLists, say, neither of the lines will compile.

jk