对于下面这段代码,我的疑问是为什么第6行会有这样的警告“Type safety: The constructor Node(Object) belongs to the raw type Node. References to generic type Node<T> should be parameterized”。
我的意思是为什么head.next是Node<T>的?(至少警告是这个意思)
明明我类里面next的类型没加泛型<T>啊@_@,求指点迷津,说得底层些也好,分不多,多包涵( ⊙ o ⊙ )啊!1  package test;

3  public class MyLinkedList {
4    public static void main(String[] args) {
5      Node<String> head = new Node<String>("黑桃10");
6      head.next = new Node("黑桃J");
7      System.out.println(head);
8    }
9  }
10 class Node<T>{
11     T value;//当前节点要保存的值
12     Node next;//下一个节点的地址
13     public Node(T value){
14       this.value = value;
15     }
16     public String toString(){
17       return next==null? value.toString() :
18                  value.toString() + "," + next.toString();
19     }
20 }

解决方案 »

  1.   

    这个Node类已经被定义为泛型类。
    任何使用这个类的地方,只要没加泛型参数,都是坯型(raw type)使用,都会被编译器警告。包括第行那个Node next。
    package test;public class MyLinkedList {  public static void main(String[] args) {
        Node<String> head = new Node<String>("黑桃10");
        head.next = new Node<String>("黑桃J");
        System.out.println(head);
      }
    }class Node<T> {  T value;//当前节点要保存的值
      Node<T> next;//下一个节点的地址  public Node(T value) {
        
        this.value = value;
      }  @Override
      public String toString() {
        
        return next == null ? value.toString() : value.toString() + "," + next.toString();
      }
    }
      

  2.   

    我的意思是为什么定义head时加了参数后导致其next也是Node<String>类型了
      

  3.   

    在你的代码中,head的next不是Node<String>,是坯型(Node)没错,但是如前所述,任何坯型的使用都会被编译器警告,编译器的警告就是说你用了坯型。
      

  4.   


    换句话说,你第12行的那句  Node next; //下一个节点的地址也属于会被警告的代码,就因为它没有为 next 指定泛型类型。
      

  5.   


    因为next对象也是使用你定义的Node类,简单来讲head和next使用的是同一个类
      

  6.   

    这个是你自己写的LinkedList吧
    首先,10 class Node<T>{
    11 T value;//当前节点要保存的值
    12 Node next;//下一个节点的地址
    13 public Node(T value){
    14 this.value = value;
    15 }
    16 public String toString(){
    17 return next==null? value.toString() :
    18 value.toString() + "," + next.toString();
    19 }
    20 }
    这是你自己写的class,并且用泛型规定了类中的type,也就是说这个Node类中属性的type已经被你统一了,不管是value还是next都是这个type。
    然后,你在main中创建对象的时候,规定了type=“String”
    5 Node<String> head = new Node<String>("黑桃10");
    不管是你的head还好,next还好,都是Node类创建的对象,所以都是String的
      

  7.   


    楼上两位属于误导。楼主如果看了1、3、4楼还不明白,那就去查查什么叫"坯型"(raw type),为什么坯型会被编译器警告。