我試圖打電話Collections.sort(c);
但我得到一個錯誤:錯誤添加泛型集合排序方法
The method sort(List<T>) in the type Collections is not applicable
for the arguments (Collection<capture#1-of ? extends E>)
這裏是我的代碼:
import java.lang.*;
public class SortedLinkedList<E extends Comparable<E>> extends LinkedList<E> {
LinkedList<Object> list = new LinkedList();
SortedLinkedList()
{
}
SortedLinkedList(Collection<? extends E> c)
{
//return sorted linked list of c
Collections.sort(c);
}
public int compareTo(Collection<? extends E> c) {
// TODO Auto-generated method stub
return 0;
}
}
我已經聲明瞭類的通用類型擴展可比接口。 但這仍然不會幫助錯誤。我跟着這篇文章被標記爲重複的帖子,但沒有多大幫助。這是我第一次嘗試在java中學習泛型。任何幫助深表感謝。 - 謝謝!
您需要的元素具有自然順序,所以它們必須是可比較的:'SortedLinkedList>擴展...' –
Andreas
'E'未定義爲'Collection extends E>',['Collections.sort' ](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#sort-java.util.List-)顯示'T'(或'E'在你的情況下)必須'Comparable','>' –
MadProgrammer
我改成 SortedLinkedList(集合<?延伸可比> C) \t { \t \t //返回的C有序鏈表 \t \t山口lections.sort(C); \t} \t 但錯誤仍然存在 –
askatral