在 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
未实现接口(interface)Comparable<VarStatusMsg>
。 sort
的签名您使用的方法是 public static <T extends Comparable<? super T>> void sort(List<T> list)
。要么让你的 VarStatusMsg
实现接口(interface)Comparable<VarStatusMsg>
,或给出 Comparator<VarStatusMsg>
作为 sort
的第二个参数.
顺便说一句。您只需调用 logMsg.sort(null)
而不是Collections.sort(logMsg)
或logMsg.sort(myNiceComparator)
而不是Collections.sort(logMsg, myNiceComparator)
。 Collections
方法仅委托(delegate)给 List
如今。
关于java - "No suitable method found for sort(List<VarStatusMsg<? extends Number>>)"JDK错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37141029/