From the Java Docs of ArrayStoreException
Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects.
To avoid this exception i used generics.But when i ran the following Test program:
public class Test {
public static void main(String[] args) {
Collection<Number> nums = new ArrayList<Number>();
nums.add(new Integer(1));
nums.add(new Long(-1));
Integer[] ints = nums.toArray(new Integer[nums.size()]);
for (Integer integer : ints) {
System.out.println(integer);
}
}
}
I am getting ArrayStoreException
as below - Which i am not expecting.
Exception in thread "main" java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at java.util.ArrayList.toArray(ArrayList.java:408)
Can some one help me understand why is this exception thrown even if my list is of generic type.