代码:
Collection c = new LinkedList();
c.add(new Integer(10));请问这样有问题吗,eclipse有如下提示,是什么意思,怎么解决?
Type safety: The method add(Object) belongs to the raw type Collection. References to generic type 
 Collection<E> should be parameterized

解决方案 »

  1.   


       我没使用过Collection 类但是它肯定是 LinkedList的扩展类,所以应该也是List的扩展类,
       那么它的ADD 方法,的参数应该是一个控件 而不是多个:
        只能是int 不能是有int 组成的一个数组。
      这是我想的。
      

  2.   

    楼上的说什么呢什么控件,什么int组成的一个数组Collection是一个接口,什么是LinkedList的扩展类、也应该是List的扩展类
      

  3.   

    yonghker(永) 不懂你在说什么。
      

  4.   

    Collection c = new LinkedList(); // 如果你已经知道你只会往里面放 Integer 就,
    Collection<Integer>c = new LinkedList<Integer>(); // 或者你可以 new LinkedList<Number>();c.add(new Integer(10));Type safety: The method add(Object) belongs to the raw type Collection. References to generic type 
    Collection<E> should be parameterized就是说你如果用参数表示的类在使用时如果给出了具体的参数的话,以后在 Cast 时就可以省略了,因为编译器能检查你当时定义变量时使用了类型参数, 以后只能往里面添加正确的类型的变量,如果变量类型不是定义的就会出错,比如上面定义集合用了 <Number> 那么以后只能往里面添加 Number 的或子类的实例,否则无法编译.
      

  5.   

    谢谢humanity(总是偷窥 Java & XML)
    解释得很明白,懂了不过我觉得这个warning没必要存在,能否对eclipse进行设置一下呢?