I would like fill an external array with the elements of my ArrayQueue via E[] toArray(E[] a) , but somehow it throws ArrayStoreException at the first System.arraycopy method. I would like to know how to solve this issue, and even more important for me to know why this exception is thrown.
Here's my code:
public E[] toArray(E[] a)
{
if(a.length != size)
a=(E[])Array.newInstance(a.getClass(), size);
if(head<tail)
System.arraycopy(elementData, head, a, 0, tail-head); // ArrayStoreException
else
{
System.arraycopy(elementData, head, a, 0, capacity-head);
System.arraycopy(elementData, 0, a, capacity-head, tail);
}
return a;
}
This is the external method:
String[] words = q.toArray(new String[2]);
Thanks for your time.
E[] elementDatawhich contains the elements. The underlying type ofelementDataisObject, and it is casted in the consturctor to(E[])