为什么Java没有';我不接受我的LinkedList的泛型,但接受它自己的?
对于类赋值,我们不能使用任何bultin类型的语言,所以我只能使用自己的列表。无论如何,情况是这样的:为什么Java没有';我不接受我的LinkedList的泛型,但接受它自己的?,java,linked-list,comparable,Java,Linked List,Comparable,对于类赋值,我们不能使用任何bultin类型的语言,所以我只能使用自己的列表。无论如何,情况是这样的: public class CrazyStructure <T extends Comparable<? super T>> { MyLinkedList<MyTree<T>> trees; //error: type parameter MyTree is not within its bound } 公共类CrazyStructure
public class CrazyStructure <T extends Comparable<? super T>> {
MyLinkedList<MyTree<T>> trees; //error: type parameter MyTree is not within its bound
}
公共类CrazyStructure我假设您的MyTree与LeftistTree相同。签名的问题是它没有实现compariable为什么链接列表必须接受键入的compariable
对于集合数据结构,强制集合只接受特定的数据类型是非常有限的。
如果您希望有一个排序的链表,最好接受任何元素,并允许您的链表接受一个比较器
对象。如果未提供比较器
,则如果所包含的元素类型为可比
类型,则可以依赖其自然顺序
请看一下或api签名以获取一些示例。我们可以看到您的MyLinkedList
定义吗?最好向我们展示MyLinkedList和MyTree的声明。编辑方法签名后,如果您需要,我可以发布其余的签名
public class CrazyStructure <T extends Comparable<? super T>> {
LinkedList<MyTree<T>> trees;
}
public class MyLinkedList<T extends Comparable<? super T>> {
private class Node<T> {
private Node<T> next;
private T data;
protected Node();
protected Node(final T value);
}
Node<T> firstNode;
public MyLinkedList();
public MyLinkedList(T value);
//calls node1.value.compareTo(node2.value)
private int compareElements(final Node<T> node1, final Node<T> node2);
public void insert(T value);
public void remove(T value);
}
public class LeftistTree<T extends Comparable<? super T>>
implements Comparable {
private class Node<T> {
private Node<T> left, right;
private T data;
private int dist;
protected Node();
protected Node(final T value);
}
private Node<T> root;
public LeftistTree();
public LeftistTree(final T value);
public Node getRoot();
//calls node1.value.compareTo(node2.value)
private int compareElements(final Node node1, final Node node2);
private Node<T> merge(Node node1, Node node2);
public void insert(final T value);
public T extractMin();
public int compareTo(final Object param);
}
public class LeftistTree<T extends Comparable<? super T>>
implements Comparable<LeftistTree<? super T>>