I am making an implementation of a generic ArrayList class that will follow the given elements natural ordering. I assume there is some way to do this, similar to how TreeSet follows the given elements' natural ordering.
When I use the .compareTo method call on the objects stored within, there is an error that says "Cannot resolve method 'compareTo' in 'E'". How do I tell the compiler that the Object E should only be classes that do implement the comparable interface?
Right now, the relevant code looks like this:
public class SortedList<E> {
...
public int indexOf(E value) {
...
else if (value.compareTo(this.get(minIndex)) > 1)...
}
}
This post was close to helping: Cannot find compareTo when receiving comparable Object[] but its for one specific static method, while I need the objects for the whole class to be Comparable, and the same addition does not seem to work for the class header.
Is there something I can add to the class header that performs a similar function?