我在 GWT 中有一个列表,我希望对其进行排序,但 GWT 在运行时提示无法完成。
List<int[]> tuples = new LinkedList<int[]>();
tuples.sort(new TupleComparator());
class TupleComparator implements Comparator<int[]> {
@Override
public int compare(int[] o1, int[] o2) {
int i = 0;
int l = o1.length;
while (i < l && o1[i] == o2[i]) {
i++;
}
return (i == l ? 0 : o1[i] - o2[i]);
}
}
[ERROR] [testgwtlistofint] - Line 116: The method sort(TupleComparator)
is undefined for the type List<int[]>
最佳答案
您显然正在调用List
接口(interface)中定义的sort()
方法。查看 Java7/Java8 的 javadocs:
List.sort()
是在 Java 8 中添加的。我的猜测是您正在尝试使用 Java 7 或更低版本运行代码。
如果出现运行时错误,则代码可能是使用 Java 8 编译的,但使用 Java 7 或更低版本执行。您的代码无法使用 Java 7 进行编译。
要在 Java 7 中进行排序,请使用 Collections.sort(tuples, new TupleComparator());
。
关于java - 如何在 GWT 中对 List<int[]> 进行排序?自定义比较器失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27588056/