I have a user-defined class that is supposed to be generic to accept Integers, Floats, Doubles, etc. but also needs to be able to be compared. It looks like:
public class MyClass<T extends Comparable<T>> implements Comparable<MyClass<T>>{
private T myClassObject;
...
And the compareTo method looks like
@Override
public int compareTo(MyClass<T> o){
return this.myClassObject.compareTo((T) o));
}
Now in a later class that is supposed to be creating an ArrayList of MyClass objects and comparing them, I get:
sort(java.util.List<T>)' in 'java.util.Collections' cannot be applied to '(java.util.ArrayList<MyClass<?>>)
The ArrayList that holds these MyClass objects looks like:
ArrayList<MyClass<?>> temp = new ArrayList<MyClass<?>>();
Is it failing because of the wildcard generic use? Do I need to specify that ? extends Number or something else?