【发布时间】:2015-07-17 09:10:04
【问题描述】:
当我执行下面的代码时,我得到的输出为[0, -2000000000, 2000000000]。
import java.util.Arrays;
import java.util.Comparator;
public class SordidSort {
public static void main(String args[]) {
Integer big = new Integer(2000000000);
Integer small = new Integer(-2000000000);
Integer zero = new Integer(0);
Integer[] arr = new Integer[] { big, small, zero };
Arrays.sort(arr, new Comparator<Object>() {
public int compare(Object o1, Object o2) {
return ((Integer) o2).intValue() - ((Integer) o1).intValue();
}
});
System.out.println(Arrays.asList(arr));
}
}
它是如何对数字进行排序的?
【问题讨论】:
-
完全按照您指定的方式进行操作,只是
int不能保存(-)4000000000的值,因此会出现溢出。尝试使用较小的数字,或long。
标签: java arrays collections comparator