I am working on generics and sorting algorithms and my professor has provided us with a template to base our sorting classes on. However, I am confused on what the following means:
public interface Sort<T extends Comparable<?>>
I have another class (MergeSort)
public class MergeSort<T extends Comparable<?>> implements Sort<T> {
however, with that class title I cannot use the follow line of code
if (l.get(leftIndex).compareTo(r.get(rightIndex)) < 0)
as the compareTo gives me an error of
The method compareTo(capture#1-of ?) in the type Comparable<capture#1-of ?>
is not applicable for the arguments (T)
but if I change the question mark of Comparable<?>
to Comparable<T>
, it can work. However, I don't understand the logic or reasons behind this if anyone can help please. Thank you!
Other information: My professor will provide us with an ArrayList<Integer>
of Integers and we must initialize our sorting classes to make that a LinkedList<T> = new LinkedList<>()
in our methods.
Thank you!