排序比较接口整形溢出问题

1. 排序比较接口整形溢出问题

    public static void main(String[] args) {
        int[][] res = new int[][]{{2147483646,2147483647},{-2147483646,-2147483645}};
        Arrays.sort(res, (a,b)->{
            return a[1]-b[1];
        });
    }
  • 该段代码想对数组按第二位升序排序,但是用a[1]-b[1]达不到效果,原因是有可能会发生溢出
  • 解决方法是将a[1]-b[1]换为Integer.compare(a[1], b[1])
posted @ 2021-02-22 19:09  骑着蜗牛追夕阳  阅读(73)  评论(0)    收藏  举报