0

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!

zerrisk
  • 33
  • 1
  • 7
  • [Wildcard Capture and Helper Methods](https://docs.oracle.com/javase/tutorial/java/generics/capture.html). – Elliott Frisch Feb 09 '17 at 04:03
  • 5
    Looks like a mistake to me. It should probably be `>`. – shmosel Feb 09 '17 at 04:04
  • Possible duplicate of [Java Generics (Wildcards)](http://stackoverflow.com/questions/252055/java-generics-wildcards) – MikaelF Feb 09 '17 at 04:22
  • @shmosel I'm not sure what to do then if it is a mistake. It's part of his package that includes a class for JUnit4 testing. – zerrisk Feb 09 '17 at 04:36
  • @shmosel I added my own "compare" method and compares two objects and made sure they were instanceof Integer or Long, etc – zerrisk Feb 09 '17 at 06:51

0 Answers0