Possible Duplicate:
Creating generic arrays in Java
I want to trim a generic array of objects down to only the first len elements. This seems like it should work:
@SuppressWarnings("unchecked")
public static <T> T[] trimArray(T[] data, int len){
    T[] result = (T[]) new Object[len];
    System.arraycopy(data, 0, result, 0, len);
    return result;
}
But it throws a ClassCastException if I do something like
public class Foo{
    double f;
    public Foo(double f){
    this.f = f;
    }
}
public static void main(String[] args){
    Foo[] A = new Foo[10];
    A[0]= new Foo(1);
    A[1]= new Foo(2);
    A[2]= new Foo(3);
    Foo[] B = trimArray(A, 3);
}
I don't understand why this won't work, but something similar does in java generic casting in generic classes
 
     
     
    