自定义java中自定义链接列表的值类

muk*_*jee 2 java linked-list

我想为自定义创建一个自定义NodeLinkedList.所述Node应包含一个value和另一个的参考Node对象.

public class Node {
   Value value;
   Node nextNode;
   public Node(Value value, Node nextNode) {
       this.value = value;
       this.nextNode = nextNode;
   }

}
Run Code Online (Sandbox Code Playgroud)

如何创建这个Value类,以便它可以获得value用户选择的任何数据类型?

Era*_*ran 6

您不需要Value类.您可以使用泛型类型参数:

public class Node<T> {
   T value;
   Node nextNode;
   public Node(T value, Node nextNode) {
       this.value = value;
       this.nextNode = nextNode;
   }
}
Run Code Online (Sandbox Code Playgroud)

您的LinkedList类还应该有一个类型参数:

public class LinkedList<T>
{
    private Node<T> head;
    ...
}
Run Code Online (Sandbox Code Playgroud)