[英]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.