我有以下程序
import java.util.*;
public class Test {
public static void main(String[] args) {
Integer[] array = { 3, 1, 4, 1, 5, 9 };
Arrays.sort(array, new Comparator<Integer>() {
public int compare(Integer i1, Integer i2) {
return i1 < i2 ? -1 : (i2 > i1 ? 1 : 0);
}
});
System.out.println(Arrays.toString(array));
}
}
这给了我输出 [3, 1, 4, 1, 5, 9]。为什么?
最佳答案
因为i1 < i2
与i2 > i1
相同- 看看你在 compareTo 方法中写了什么。
关于java - 使用比较器的意外输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8900871/