Unckecked方法调用上Collections.sort()
警告我想排序收为自变量的List<MyType>
:Unckecked方法调用上Collections.sort()
static void doSomething(List<MyType> arg) {
Collections.sort(arg);
}
...但我得到这样的警告:
Unchecked method 'sort(List<T>)' invocation
这里的MyType
:
class MyType implements Comparable {
private int number;
public MyType(int n) {
number = n;
}
public int compareTo(MyType b) {
return Integer.compare(number, b.number);
}
}
我可以禁止这个警告,但我想知道我在做什么w荣。
@Aominè说得对:MyType
实施了Comparable
而不是Comparable<MyType>
。现在我已经改变了这个警告。
最重要的是,原因是因为'Collections.sort(列表
请参阅此处的方法的完整定义 - > [** Collections.sort(List
您的示例代码似乎没有与警告马赫。请显示确切的代码。 – davidxxx
您应该编写一个能够重现问题的[MCVE]。 – pvg
这很可能是因为'MyType'不使用通用的'Comparable'。所以要移除警告只需实现通用的“Comparable”接口。 –