Anmol
Anmol

Reputation: 91

inference variable T has incompatible bounds Error

ArrayList<ShipDetail> detailArray = new ArrayList<ShipDetail>(Arrays.asList(shipDetail));
Sorter.QuickSort( detailArray );

And this is my Sorter class in which I was trying to do Implement some algorithms.

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

}

But while compiling i am getting the following error :

required: AbstractList found: ArrayList reason: inference variable T has incompatible bounds equality constraints: ShipDetail upper bounds: Comparable where T is a type-variable: T extends Comparable declared in method QuickSort(AbstractList)

Upvotes: 2

Views: 6167

Answers (1)

mszymborski
mszymborski

Reputation: 1654

ShipDetail is not comparable to itself. The bound here:

<T extends Comparable<T>>

Also applies to the type variable T in the argument, which is inferred as ShipDetail.

ShipDetail should be defined as follows:

class ShipDetail implements Comparable<ShipDetail> { ...

And not:

class ShipDetail implements Comparator<ShipDetail> { ...

Comparators are objects-algorithms which provide service of comparison of other objects, while Comparables are objects which themselves allow comparing them to other kind of objects.

Upvotes: 6

Related Questions