为什么 Java 不接受我的泛型 LinkedList,却接受它自己的 LinkedList?
对于课堂作业,我们不能使用任何类型的语言,因此我只能使用自己的列表。不管怎样,情况是这样的:
public class CrazyStructure <T extends Comparable<? super T>> {
MyLinkedList<MyTree<T>> trees; //error: type parameter MyTree is not within its bound
}
但是:
public class CrazyStructure <T extends Comparable<? super T>> {
LinkedList<MyTree<T>> trees;
}
有效。 MyTree 实现了 Comparable 接口,但 MyLinkedList 没有。然而,根据 这个。那么问题是什么以及如何解决它?
我的链接列表:
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设你的 MyTree 与 LeftistTree 相同。签名的问题在于它没有实现Comparable>。
所以签名应该是:
原因是你的MyLinkedList不像常规的LinkedList。常规 LinkedList 的类型为:
LinkedList
T 上没有界限。您要求 MyLinkedList 的参数实现其自身(或其超类)的 Comparable,但实际上 LeftistTree 正在实现一个raw Comparable(或Comparable
),因此不能保证 Comparable 与类型相关。I assume your MyTree is the same as LeftistTree. The problem with the signature is that it doesn't implement
Comparable<LeftistTree<? super T>>
.So the signature should be:
The reason is that your MyLinkedList is not like a regular LinkedList. A regular LinkedList is of type:
LinkedList<T>
there are no bounds on T. You require with MyLinkedList that the parameter implement a Comparable of itself (or its superclass), but in fact LeftistTree was implementing a raw Comparable (orComparable<?>
) so the Comparable was not guaranteed to be related to the type.为什么您的链接列表必须接受
Comparable
类型?对于集合数据结构,强制集合仅接受特定数据类型是非常有限的。
如果您想要一个排序的链表,最好接受任何元素并允许您的链表接受
Comparator
对象。如果您不提供Comparator
,那么您可以依赖所包含元素的自然排序(如果它们是Comparable
类型)。看一下 SortedSet< /a> 或 SortedMap api 签名一些例子。
Why does your linked list must accept a
Comparable
typed?For a collection data structure, forcing your collection to only accept specific data type is very limiting.
If you would like to have a sorted linked list, it is better to accept any element and allow your linked list to accept a
Comparator
object. If you do not provide aComparator
, then you can rely on the natural ordering of the contained element if they are ofComparable
typed.Take a look at the SortedSet or SortedMap api signature for some example.