小弟今天做了一个练习,但总是警告……
public class MyArrayList<T> { private T[] arr; private int index; public MyArrayList() {
arr = (T[]) new Object[10];  // 警告的就是这行
index = 0;
}
}
在eclipse中总是有个黄线
报的是:Type safety: The cast from Object to T is actually checking against the erased type Object请教各路高手。
谢谢

解决方案 »

  1.   

    Type safety: The cast from Object[] to T[] is actually checking against the erased type Object[]
      

  2.   

    这样做无法按预期转换成T[]类型是由于Type Erasure,Type erasure就是为了保证与此前的java库与应用程序保持(二元)兼容
    在CSDN以前有过这样的帖子:http://topic.csdn.net/u/20080222/12/8f70b9aa-7c35-400f-b79e-6335dfe17d59.html
      

  3.   

    Object[]->T[],只有当T为Object才可以,其他都不行 
    警告是小事
    但错误就是大事了
    你需要再思考你的代码
      

  4.   

    对于强制类型转换(Object)错误的情况,编译器可能不提示错误,在运行的时候才出现异常,这是一个安全隐患。泛型的好处是在编译的时候检查类型安全。这不,你就发现问题了.
      

  5.   

    编译器认为T和Object 不是同一个类型
    所有有提示吧
      

  6.   

    编译器认为T和Object 不是同一个类型
    所有有提示吧
      

  7.   

    编译器认为T和Object 不是同一个类型
    所有有提示吧