'Extend a generic class in java with a particular type [duplicate]
I'm new in java and I just discovered generics. I thought i understood the principle till i tested this code. I wanted to create a class SortedListOfStrings, extending ArrayList that would insert an element in lexical order in the list.
public class SortedListOfStrings<String> extends ArrayList<String> {
public boolean add(String s) {
boolean done = false;
int i;
for(i=0; i<this.size(); i++) {
if(get(i).compareTo(s) > 0) {
this.add(i,s);
done = true;
}
}
if(!done) {
this.add(i, s);
}
return true;
}
}
But I caught this error : The method compareTo(String) is undefined for the type String SortedListOfStrings.java line 11
Can someone explain me why I do have it or what I did wrong ?
Thanks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
