2017-01-31 38 views
0
ArrayList<ShipDetail> detailArray = new ArrayList<ShipDetail>(Arrays.asList(shipDetail)); 
Sorter.QuickSort(detailArray); 

而這是我在其中嘗試執行一些算法的Sorter類。推理變量T具有不兼容的邊界錯誤

public class Sorter 
{ 
    public static<T extends Comparable<T>> void QuickSort(AbstractList<T> collection) 
    { 
     quickSort(collection,0,collection.size()-1); 
    } 

} 

但是在編譯時我收到以下錯誤:

要求:AbstractList的 發現:ArrayList的 原因:推論變量T有不兼容的界限 等式約束:ShipDetail 上限:可比 其中T是一個類型變量: T擴展Comparable在方法QuickSort(AbstractList)中聲明

+2

是'ShipDetail'媲美本身?這是必需的(注'>') – mszymborski

+0

ü意味着,如果我有覆蓋比較方法,如果是的話是 – Anny

+0

顯示ShipDetail'的'類的聲明。 – shmosel

回答

2

ShipDetail不可比較本身。這裏所結合的:

<T extends Comparable<T>>

也適用於類型的參數,它被推斷爲ShipDetail可變T

class ShipDetail implements Comparable<ShipDetail> { ... 

而且不是:

ShipDetail應該被定義如下

class ShipDetail implements Comparator<ShipDetail> { ... 

Comparator s爲它們提供的其他對象的比較的服務,同時Comparable s爲對象的對象的算法,其本身允許將它們與其他類型的對象進行比較。

+0

現在感謝我瞭解使用Comparator和Comparable的區別。 – Anny

+0

@Anny爽!如果你認爲它完全回答你的問題,你可以標記與綠色對勾答案。 – mszymborski

相關問題