在Java源代码上运行Ant构建时遇到以下错误:

[javac] /home/path/to/my/java/function/VarStatusLog.java:28: error: no suitable method found for sort(List<VarStatusMsg<? extends Number>>)
[javac]         Collections.sort(logMsg);
[javac]                    ^
[javac]     method Collections.<T#1>sort(List<T#1>) is not applicable
[javac]       (inference variable T#1 has incompatible bounds
[javac]         equality constraints: VarStatusMsg<? extends Number>
[javac]         upper bounds: VarStatusMsg<CAP#1>,Comparable<? super T#1>)
[javac]     method Collections.<T#2>sort(List<T#2>,Comparator<? super T#2>) is not applicable
[javac]       (cannot infer type-variable(s) T#2
[javac]         (actual and formal argument lists differ in length))
[javac]   where T#1,T#2 are type-variables:
[javac]     T#1 extends Comparable<? super T#1> declared in method <T#1>sort(List<T#1>)
[javac]     T#2 extends Object declared in method <T#2>sort(List<T#2>,Comparator<? super T#2>)
[javac]   where CAP#1 is a fresh type-variable:
[javac]     CAP#1 extends Number from capture of ? extends Number


我在我的Ant版本中使用JDK 1.8。请注意,Windows上的Eclipse不会引发错误,但是当我从Linux上的命令行运行时,会出现错误。有人可以帮助我吗?提前致谢!

最佳答案

我会说VarStatusMsg没有实现接口Comparable<VarStatusMsg>。您正在使用的sort方法的签名是public static <T extends Comparable<? super T>> void sort(List<T> list)。使您的VarStatusMsg实现接口Comparable<VarStatusMsg>,或给Comparator<VarStatusMsg>作为sort的第二个参数。

顺便说一句。您可以简单地调用logMsg.sort(null)代替Collections.sort(logMsg)logMsg.sort(myNiceComparator)代替Collections.sort(logMsg, myNiceComparator)。如今,Collections方法仅委托给List

关于java - “找不到合适的排序方法(List <VarStatusMsg <?扩展Number >>)” JDK错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37141029/

10-09 05:12