我是 Java 的新手,正在尝试实现扩展 GeneralList 接口(interface)的 MyLinkedList,我想为我的节点使用可比较的接口(interface)来保持我的列表排序, 当我尝试创建头节点时出现错误
请在以下代码下方找到错误信息
//List interface
public interface GeneralList<T>
{
public boolean addNode(T elem);
public boolean deleteNode(T elem);
public T containsNode(T elem);
public void printSll();
}
//ListImplementation
public class SLL2<T> implements GeneralList<T>
{
private static class Node<T extends Comparable<T>>
{
public T data;
public Node<T> next;
public Node()
{
data = null;
next = null;
}
}
public SLL2()
{
head = null;
}
/* 1. Error while creating a head referance*/
private Node<T> head;
@Override
public boolean addNode(T elem)
{
Node<T> tmp = new Node<T>();
tmp.data = elem;
if(head == null)
{
head = temp;
return true;
}
else
{
for(Node<T> cur = head; cur.next != null ; cur= cur.next)
{
/* iterate and add the node */
if(temp.elem.comparTo(cur.elem))
{
}
}
}
}
1. I am not able to create the head node with the declaration private Node<T> head;
It is giving error "Bound mismatch: The type T is not a valid substitute for the bounded parameter <T extends Comparable<T>> of the type SLL2<T>.Node<T>"
Please help me to resolve this error...
最佳答案
你的类(class) SLL2<T>
还应该对 T
的可比性有约束.像那样:
public class SLL2<T extends Comparable<T>> implements GeneralList<T> {
// ...
关于java - Java中的可比实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22284051/