user1743514 user1743514 - 1 month ago 18
Java Question

Type mismatch: cannot convert from Comparator<capture#2-of ? super T> to Comparator<? super T>

public class MergeSort<T> {
private Comparator<? super T> c;

<T> void sort(T[] a, Comparator<? super T> c) {
mergeSort(a, 0, a.length);
this.c = c;
}
}


Getting compilation error at
this.c = c
. Error is "Type mismatch: cannot convert from Comparator to Comparator"

Answer

The <T> on the sort method is shadowing the one in MergeSort<T>, so they don't coincide, they're two independent types that happen to be both refered to as T. Just remove it and it will work:

 void sort(T[] a, Comparator<? super T> c) {

        this.c = c;
 }