我正在尝试在自定义对象的ArrayList上使用Collections.sort,但我收到警告,我无法弄清楚为什么
Warning: Type safety: Unchecked invocation
sort(ArrayList<CharProfile>) of the generic method sort(List<T>)
of type Collections
Run Code Online (Sandbox Code Playgroud)
使用此代码:
ArrayList<CharProfile> charOccurrences = new ArrayList<CharProfile>();
...
Collections.sort(charOccurrences);
Run Code Online (Sandbox Code Playgroud)
这是我的方法:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Sub*_*der 26
Comparable应该用type来实现,这里Type是<CharProfile>
.
public class CharProfile implements Comparable<CharProfile>{
@Override
public int compareTo(CharProfile cp) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7775 次 |
最近记录: |