This question already has an answer here:
I have code that looks like this:
int[] ho = new int[10];
ho[0]= 1;
ho[2]= 1;
ho[4]= 5;
HashSet<Integer> hs = new HashSet(Arrays.asList(ho));
Integer[] a = hs.toArray(new Integer[hs.size()]);
This code makes perfect sense to me, but it throws an ArrayStoreException
when I run it. Why is that? The HashSet
is a set of Integer
s, and so is the output array.
The problem is that you are skipping generic types so you don't see the error and Java is not able to reject your code even if it's wrong.
The problem is that the signature of
Arrays::asList
isasList(T... values)
, butT
can't be a primitive so the variadic arguments are collapsed into anint[]
(which is now an object) andArrays.asList
returns aList<int[]>
.Then pass it to a generic
HashSet
constructor which then accepts aCollection<Object>
without any problem, and assign it to anHashSet<Integer>
, with the compiler warning you about using raw types.Finally you try to assign the elements in the hash set (which are of type
int[]
) to elements in anInteger[]
array causing the exception, that's like doingwhich is wrong but Java can't realize it at compile time.
If you had constructed the
HashSet
throughnew HashSet<>
then Java compiler would have risen an error. You can solve the issue by passing anInteger[]
toasList
method such that it's correctly treated as variadic arguments.ArrayStoreException
: "if the runtime type of the specified array is not a supertype of the runtime type of every element in this set"ArrayStoreException
is thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects.Try switching
int[] to Integer[]