I'm trying to use Collections.sort on a ArrayList of custom objects, but I'm getting a warning and I can't figure out why
Warning: Type safety: Unchecked invocation
sort(ArrayList<CharProfile>) of the generic method sort(List<T>)
of type Collections
With this code:
ArrayList<CharProfile> charOccurrences = new ArrayList<CharProfile>();
...
Collections.sort(charOccurrences);
And here's my method:
public class CharProfile implements Comparable {
...
@Override
public int compareTo(Object o) {
if (this.probability == ((CharProfile)o).getProbability()) {
return 0;
}
else if (this.probability > ((CharProfile)o).getProbability()) {
return 1;
}
else {
return -1;
}
}
}
Comparable should be implemented with type safety, here it is <CharProfile>
.
public class CharProfile implements Comparable<CharProfile>{
@Override
public int compareTo(CharProfile cp) {
...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With