简体   繁体   English

Java泛型:Comparable类型的compareTo(capture#2-of?扩展E)方法不适用于自变量(Comparable)

[英]Java Generics :The method compareTo(capture#2-of ? extends E) in the type Comparable is not applicable for the arguments (Comparable)

I'm writing generic code to sort the stack but getting warning while calling comareTo: 我正在编写通用代码来对堆栈进行排序,但是在调用comareTo时收到警告:

public class StackUtility {
    public static <E extends Comparable<? extends E>> void sort(IStack<E> stack) {
        IStack<E> stack2 = new MyLinkedStack<>();
        while (!stack.isEmpty()) {
            Comparable<? extends E> e = stack.pop();
            while (stack2.isEmpty() && stack2.peek().compareTo(e)>0) {
                // some code to be written
            }
        }
    }
}

Note: The return type of pop and peek is E. 注意:pop and peek的返回类型为E。

while calling compareTo I'm getting this warning from Eclipse: 在调用compareTo时,我从Eclipse得到以下警告:

The method compareTo(capture#2-of ? extends E) in the type Comparable is not applicable for the arguments (Comparable) Comparable类型的compareTo(capture#2-of?扩展E)方法不适用于自变量(Comparable)

whats the write way to do that without warning. 在没有警告的情况下如何写的方式。

E only has to implement Comparable<E> : E只需要实现Comparable<E>

public static <E extends Comparable<E>> void sort(IStack<E> stack) {
    IStack<E> stack2=new MyLinkedStack<>();

    while(!stack.isEmpty()){
        E e=stack.pop();
        while(stack2.isEmpty() && stack2.peek().compareTo(e)){
            //some code to be written
        }
    }
}

暂无
暂无

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

相关问题 java generics - Comparable 类型中的方法 compareTo(capture#1-of?)<capture#1-of ?> 不适用于 arguments</capture#1-of> - java generics - The method compareTo(capture#1-of ?) in the type Comparable<capture#1-of ?> is not applicable for the arguments Java泛型-类型T中的方法validate(T,T)不适用于自变量(可比较,可比较 <capture#11-of ?> ) - java generics - The method evaluate(T,T) in the type T is not applicable for the arguments (Comparable, Comparable<capture#11-of ?>) 验证器类型中的方法validate(capture#2-of?extended Object) <capture#2-of ? extends Object> 不适用于参数(字符串) - The method validate(capture#2-of ? extends Object) in the type Validator<capture#2-of ? extends Object> is not applicable for the arguments (String) Java Generics Comparable | 实现compareTo - Java Generics Comparable | implement compareTo Java泛型编译错误-方法method(Class <capture#1-of ? extends Interface> ) <type> 不适用于参数 - Java generics compilation error - The method method(Class<capture#1-of ? extends Interface>) in the type <type> is not applicable for the arguments 该方法在GenericDao类型中读取(捕获#2-of?) <Order,capture#2-of ?> 不适用于参数(长) - The method read(capture#2-of ?) in the type GenericDao<Order,capture#2-of ?> is not applicable for the arguments (Long) 未定义 Comparable 类型的 compareTo(Comparable) 方法 - The method compareTo(Comparable) is undefined for the type Comparable 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>> Java Generics E扩展了Comparable <E> 留下警告 - Java Generics E extends Comparable<E> leaves warning Java可比接口的compareTo方法 - Java Comparable Interface compareTo method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM