[英]How come Java doesn't accept my LinkedList in a Generic, but accepts its own?
For a class assignment, we can't use any of the languages bultin types, so I'm stuck with my own list. 对于班级分配,我们不能使用bultin类型的任何语言,因此我只能使用自己的列表。 Anyway, here's the situation: 无论如何,这是情况:
public class CrazyStructure <T extends Comparable<? super T>> {
MyLinkedList<MyTree<T>> trees; //error: type parameter MyTree is not within its bound
}
However: 然而:
public class CrazyStructure <T extends Comparable<? super T>> {
LinkedList<MyTree<T>> trees;
}
Works. 作品。 MyTree impleements the Comparable interface, but MyLinkedList doesn't. MyTree实现Comparable接口,但MyLinkedList没有。 However, Java's LinkedList doesn't implement it either, according to this . 但是,根据this ,Java的LinkedList也不实现它。 So what's the problem and how do I fix it? 那么问题是什么,我该如何解决?
MyLinkedList: 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);
}
MyTree: MyTree:
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);
}
I assume your MyTree is the same as LeftistTree. 我假设您的MyTree与LeftistTree相同。 The problem with the signature is that it doesn't implement Comparable<LeftistTree<? super T>>
签名的问题在于它没有实现Comparable<LeftistTree<? super T>>
Comparable<LeftistTree<? super T>>
. Comparable<LeftistTree<? super T>>
。
So the signature should be: 因此签名应为:
public class LeftistTree<T extends Comparable<? super T>>
implements Comparable<LeftistTree<? super T>>
The reason is that your MyLinkedList is not like a regular LinkedList. 原因是您的MyLinkedList与常规的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 (or Comparable<?>
) so the Comparable was not guaranteed to be related to the type. 常规LinkedList的类型为: LinkedList<T>
在LinkedList<T>
上没有界限。您需要使用MyLinkedList来使参数实现自身(或它的超类)的Comparable,但实际上LeftistTree在实现原始的Comparable(或Comparable<?>
),因此不保证Comparable与类型相关。
Why does your linked list must accept a Comparable
typed? 为什么您的链表必须接受Comparable
类型?
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. 如果您想要一个排序的链表,则最好接受任何元素,并允许链表接受Comparator
对象。 If you do not provide a Comparator
, then you can rely on the natural ordering of the contained element if they are of Comparable
typed. 如果不提供Comparator
,则如果它们属于Comparable
类型,则可以依靠所包含元素的自然顺序。
Take a look at the SortedSet or SortedMap api signature for some example. 看看一些示例的SortedSet或SortedMap api签名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.