![](/img/trans.png)
[英]java generics - The method compareTo(capture#1-of ?) in the type Comparable<capture#1-of ?> is not applicable for the arguments
[英]Java Generics :The method compareTo(capture#2-of ? extends E) in the type Comparable is not applicable for the arguments (Comparable)
我正在編寫通用代碼來對堆棧進行排序,但是在調用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
}
}
}
}
注意:pop and peek的返回類型為E。
在調用compareTo時,我從Eclipse得到以下警告:
Comparable類型的compareTo(capture#2-of?擴展E)方法不適用於自變量(Comparable)
在沒有警告的情況下如何寫的方式。
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.