Question

I'm using Netbeans. When I run the program below, I get this as output [I@de6ced! How come?

import java.util.Arrays;
import java.util.Vector;

public class Test {

public static void main (String[] args) {
     int[] a = new int[1];
     a[0] = 5;
     Vector<Integer> a1 = new Vector(Arrays.asList(a));
     System.out.println(a1.elementAt(0));
 }
}

I also tried working around it but then I got a

Exception in thread "main" java.lang.ClassCastException: [I cannot be cast to java.lang.Integer at TopCoder.Test.main(Test.java:13) Java Result: 1

public static void main (String[] args) {
    int[] a = new int[1];
    a[0] = 5;
    Vector<Integer> a1 = new Vector(Arrays.asList(a));

    int b = a1.elementAt(0); /* EXCEPTION THROWN HERE */
    System.out.println(b);
}
Was it helpful?

Solution

Integer[] a = new Integer[1];
a[0] = new Integer(5);
List list = Arrays.asList(a);
System.out.println(list.get(0));

The above works as you would expect.

So it looks like the "int" array is treated like an Object and not an array of Integers. In other words auto boxing doesn't seem to be applied to it?

OTHER TIPS

[I@de6ced can be broken down as:
- [ an array
- I of integers
- de6ced with this reference hash-code (which, in Sun Java, is basically the reference)

toString() for Object returns somethine like ClassName@HashCode, which is exactly what you're seeing happen here just with the (rather wierd) primitive-array classes.

The problem is that the wrong type is being inferred by the <T> List<T> asList(T...) method. Change your code to use Integer[] instead of int[]. This is a consequence of int being primitive, but int[] is an object.

You can see this directly:

System.out.println(Arrays.asList(new int[]{5}));

=> [[I@some reference

System.out.println(Arrays.asList(new Integer[]{5}).get(0));

=> 5

I think I have figured out what was happening:

 int[] a = new int[1];
 a[0] = 5;

We now have an array of int.

 Vector<Integer> a1 = new Vector(Arrays.asList(a));

The problem is in the way you are calling Arrays.asList.

The signature for asList is "public static <T> List<T> asList(T... a)". It does not apply with T == int because int is not an object type. And it cannot match with T == Integer because the base type of the array a is int not Integer. What is actually happening is that T is binding to int[], and Arrays.aslist(a) is returning a List<int[]> with one element that is the value of a!!!

Then you create a Vector from the List and get a Vector with one element ... the original int[] that was assigned to 'a'.

 System.out.println(a1.elementAt(0));

Finally, a1.elementAt(0) fetches the int[], and you end up calling the Object implementation of the toString() method.

A couple of important lesson to learn from this:

  • it is a bad idea to mix raw types and generic types as you do on the line that declares a1, and
  • it is a bad idea to ignore, or turn off the compiler's generic type-safety warnings

It looks like the int's are becoming Integers using autoboxing so you are getting an object reference instead of the value. Still seems weird as it should call the correct toString and end up with "5".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top