can you try result.toArray(new Cookie[2]);
instead of (Cookie[]) result.toArray();
?
Reason being - calling just result.toArray();
returns Object[]
If you see the source code of ArrayList
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
and
public <T> T[] toArray(T[] a) {
if (a.length < size)
// Make a new array of a's runtime type, but my contents:
return (T[]) Arrays.copyOf(elementData, size, a.getClass());
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
here the latter, clearly uses Arrays.copyOf
passing the type of array
And Object[]
can't be cast to Cookie[]