如何在GWT中对List <int []>进行排序?自定义Comparator </int []>失败

时间:2014-12-21 09:09:34

标签: java gwt

我在GWT中有一个List,我希望对它进行排序,但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[]>

1 个答案:

答案 0 :(得分:3)

您显然正在调用sort()接口中定义的方法List。看一下Java7 / Java8的javadoc:

在Java 8中添加了

List.sort()。我猜您正在尝试使用Java 7或更低版​​本运行代码。

如果遇到运行时错误,代码可能是用Java 8编译的,但是用Java 7或更低版​​本执行。您的代码无法使用Java 7进行编译。

要在Java 7中进行排序,请使用Collections.sort(tuples, new TupleComparator());

相关问题