注意,是JDK1.5
自己写了一个:package vec.server.util;import java.util.ArrayList;public class ArraysCopyer {    public static <T> T[] copyOf(T[] original,
                                 int newLength) {
        if (newLength < 0) {
            throw new NegativeArraySizeException();
        }        if (original == null) {
            throw new NullPointerException();
        }        ArrayList<T> target = new ArrayList<T>(newLength);
        for (int i = 0; i < newLength; ++i) {
            target.add(i, original[i]);
        }
        for (int i = newLength; i < original.length; ++i) {
            target.add(i, null);
        }        return (T[])(target.toArray());
    }    private ArraysCopyer() { };}使用的时候:
AttrToken[] attrs = ArraysCopyer.copyOf(orig, count);结果异常:
ClassCastException

解决方案 »

  1.   

    我测试的没问题
      public static void main(String[] args) {
        String[] list = { "1", "2", "3", "4" };
        Object[] os = copyOf(list, 3);
        for (Object o : os) {
          System.out.println((String)o);
        }
      }
      

  2.   

    造型异常出在什么位置啦
    ——AttrToken[] attrs = ArraysCopyer.copyOf(orig, count); 我测试的没问题 
    ——你是JDK1.5吗?
      

  3.   

    AttrToken[] attrs = ArraysCopyer.copyOf(orig, count); 楼主不能这样使用的
    ArraysCopyer.copyOf返回的object[],并不是你想象的AttrToken[]楼主这样定义使用泛型的意义不大
    不要返回数组,定义和返回arraylist<T>会比较有意义