[英]Bound mismatch: The type is not a valid substitute for the bounded parameter <E extends Comparable<E>> of the type
I have a class:我有一个 class:
public class BinarySearchTree<E extends Comparable<E>> {
private TreeNode<E> root;
//other methods like insert, etc
}
public class TreeNode<V extends Comparable<V>> implements Comparable<V>{
//left and right instances of TreeNode and data holder V instance.
//insert method, etc
@Override
public int compareTo(V o) {
return 0;
}
}
So far so good.到目前为止,一切都很好。
When I write this code with in class BinarySearchTree
,:当我在 class
BinarySearchTree
中编写此代码时,:
public void levelOrderTraversalQueue() {
Queue<TreeNode<E>> q = new Queue(); //<--Bound mismatch: The type BinarySearchTree<E>.TreeNode<E> is not a valid substitute for the bounded parameter <E extends Comparable<E>> of the type Queue<E>
}
Why is that?这是为什么? My Queue class is implemented like this:
我的队列 class 是这样实现的:
public class Queue<E extends Comparable<E>> {
private LinkedList<E> list;
//other operations
}
How do I solve this error?我该如何解决这个错误?
This:这个:
public class TreeNode<...> implements Comparable<V>{
means that a TreeNode
can be compared to a V
- the thing it is storing.意味着可以将
TreeNode
与V
进行比较 - 它正在存储的东西。
That's not what you mean.这不是你的意思。 To be able to put a tree node into your
Queue
, a tree node needs the ability to be compared with another tree node .为了能够将一个树节点放入您的
Queue
中,一个树节点需要能够与另一个树节点进行比较。 Recall:记起:
Queue<E extends Comparable<E>>
Whatever E
is, it must implement Comparable<E>
.无论
E
是什么,它都必须实现Comparable<E>
。 Here, E
is TreeNode<V>
.在这里,
E
是TreeNode<V>
。
Therefore you should write:因此你应该写:
public class TreeNode<...> implements Comparable<TreeNode<V>> {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.