2015-06-18 125 views
2

我想为自定义LinkedList定制一个自定义Node类。 Node应包含一个value和对另一个Node对象的引用。在java中自定义链接列表的自定义值类

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

} 

如何让我这个Value类,以便它可以让用户自己选择的任何数据类型的value

+0

使用泛型类型PARAMS – Abhi

回答

6

您不需要Value类。您可以使用一个通用的类型参数:

public class Node<T> { 
    T value; 
    Node nextNode; 
    public Node(T value, Node nextNode) { 
     this.value = value; 
     this.nextNode = nextNode; 
    } 
} 

LinkedList类也应该有一个类型参数:

public class LinkedList<T> 
{ 
    private Node<T> head; 
    ... 
}