The following code is from Arrays
class:
http://developer.classpath.org/doc/java/util/Arrays-source.html
public static <T,U> T[] copyOfRange(U[] original, int from, int to,
Class<? extends T[]> newType)
{
if (from > to)
throw new IllegalArgumentException("The initial index is after " +
"the final index.");
T[] newArray = (T[]) Array.newInstance(newType.getComponentType(),
to - from);
if (to > original.length)
{
System.arraycopy(original, from, newArray, 0,
original.length - from);
fill(newArray, original.length, newArray.length, null);
}
else
System.arraycopy(original, from, newArray, 0, to - from);
return newArray;
}
I'm baffled by the Class<? extends T[]>
part, how can T[]
be extended? As far as I know there's no such array like K[]
that is subtype of T[]
even though K extends T
.