简体   繁体   English

有界不匹配:类型不是有界参数的有效替代品<e extends comparable<e> &gt; 类型</e>

[英]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.意味着可以将TreeNodeV进行比较 - 它正在存储的东西。

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> .在这里, ETreeNode<V>

Therefore you should write:因此你应该写:

public class TreeNode<...> implements Comparable<TreeNode<V>> {

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Generics - 绑定不匹配:类型 Object 不是绑定参数的有效替代品<e extends comparable<e> &gt;</e> - Java Generics - Bound mismatch: The type Object is not a valid substitute for the bounded parameter <E extends Comparable<E>> 为什么“类型绑定不匹配:类型? 扩展T不是有界参数的有效替代品 <E extends Enum<E> &gt;枚举类型 <E> “? - Why ”Type Bound mismatch: The type ? extends T is not a valid substitute for the bounded parameter <E extends Enum<E>> of the type Enum<E>“? JAVA:绑定不匹配:不是有界参数的有效替代 <E extends Comparable<E> &gt; - JAVA : Bound mismatch: is not a valid substitute for the bounded parameter <E extends Comparable<E>> 绑定不匹配:该类型不是绑定参数的有效替代 - Bound mismatch: The type is not a valid substitute for the bounded parameter 边界不匹配:类型A不是边界参数的有效替代品 <T extends Entity> 类型为TestService <T> - Bound mismatch: The type A is not a valid substitute for the bounded parameter <T extends Entity> of the type TestService<T> 绑定不匹配:MyClass1类型不是绑定参数的有效替代品 <T extends Comparator<T> &gt;类型的人 <T> - Bound mismatch: The type MyClass1 is not a valid substitute for the bounded parameter <T extends Comparator<T>> of the type Person<T> Java泛型:绑定不匹配:类型不是该类型的bounded参数的有效替代 - Java generics: Bound mismatch: The type is not a valid substitute for the bounded parameter of the type Java泛型,绑定不匹配:类型不是bounded参数的有效替代品 - Java generics , bound mismatch: The type is not a valid substitute for the bounded parameter Java泛型绑定不匹配:类型不是边界参数的有效替代品 - java generics Bound mismatch: The type is not a valid substitute for the bounded parameter Eclipse中有限的不匹配。 “该类型不是有界参数的有效替代” - Bounded mismatch in Eclipse. “The type is not a valid substitute for the bounded parameter”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM