Arrays.copyOfRange()

I have a simple Class object that is known only at runtime. How can I turn that class to "Class<? extends T[]>" to pass as the last parameter of Arrays.copyOfRange().
[179 byte] By [Gen.Javaa] at [2007-11-26 22:48:29]
# 1

I didnt even know that method existed (Java 6 only).

http://java.sun.com/javase/6/docs/api/java/util/Arrays.html

Why is this method (and its 10 variants - 20 if you count CopyOf) even necessary?

It does exactly the same thing as System.arraycopy

except that it creates a new array instead of being passed one.

At most that saves you one line of code (a new array initialization).

int[] source...;

int[] target...;

System.arraycopy(source, 0, target, 0, source.length);

vs

int[] source...;

int[] target = Arrays.copyOfRange(source, 0, source.length)

Isnt this just bloating the API for no good reason at all?

I mean all theyve done is make 10 copies of a method that already exists.

TuringPesta at 2007-7-10 12:08:18 > top of Java-index,Java Essentials,Java Programming...
# 2

As for the original question: if your class object is already a subclass of T[] then you don't have to do anything. But if it is not, then there is nothing you can do to cause it to be one.

Maybe you could ask your earlier question, the one which is the background to this one. Because this one doesn't really make any sense to me.

DrClapa at 2007-7-10 12:08:18 > top of Java-index,Java Essentials,Java Programming...
# 3

TuringPest:

I meant using the following method:

static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType)

This method is great. It instantiates a new array, copies elements to it, and controls the destination array type. No existing method provide all this.

DrClap:

I simply need to create an array of type T (known at runtime). So the class passed could be String[].class, Integer[].class, Object[].class, ... etc. I tested it with hardcoding the type and it worked.

I think the problem is with the syntax rather than the relationship between the class object and T[] because I am getting a compilation error not a runtime error.

Gen.Javaa at 2007-7-10 12:08:18 > top of Java-index,Java Essentials,Java Programming...