I have the following code which merges two arrays and should handle any type apart from primitives.
@SuppressWarnings("unchecked")
public static synchronized <E> E[] aryMergeNoDup(E[]... arys){
HashSet<E> hs = new HashSet<E>();
for(E[] ary : arys) {
if(ary == null) continue;
for(E item : ary) {
if(item != null) hs.add(item);
}
}
hs.remove(null);
return hs.toArray((E[]) Array.newInstance(
arys.getClass().getComponentType(),hs.size()));
}
However when the code runs it generates this exception:
java.lang.ArrayStoreException: java.lang.String
at java.util.AbstractCollection.toArray(AbstractCollection.java:188)
at util.Utils.aryMergeNoDup(Utils.java:197)
The runtime type of the variable arys is String[]; however, when I replace arys.getClass().getComponentType() with String.class the code runs fine.
However, the method can only be used for Strings because pf this. I can't see what's going wrong, as they should both refer to java.lang.String.
The line in AbstractCollection.java:188 that throws the Exception is:
r[i] = (T)it.next();
public <T> T[] toArray(T[] a) {
// Estimate size of array; be prepared to see more or fewer elements
int size = size();
T[] r = a.length >= size ? a :
(T[])java.lang.reflect.Array
.newInstance(a.getClass().getComponentType(), size);
Iterator<E> it = iterator();
for (int i = 0; i < r.length; i++) {
if (! it.hasNext()) { // fewer elements than expected
if (a != r)
return Arrays.copyOf(r, i);
r[i] = null; // null-terminate
return r;
}
r[i] = (T)it.next();
}
return it.hasNext() ? finishToArray(r, it) : r;
}