洛上言 2023-06-22 17:55 采纳率: 95.4%
浏览 20
已结题

这里为啥报红线?该如何修改?

这里为啥报红线?该如何修改?

img


完整代码:

package test;

import java.util.Arrays;

public class test {
    public static void main(String[] args) {
        int[] ans = {3, 4, 5, 7, 8, 10};
        Arrays.sort(ans, new Comparator(){
            @Override
            public int compare(Object o1, Object o2) {
                int i1 = (Integer) o1;//拆箱
                int i2 = (Integer) o2;
                return i1 - i2;
            }
        }));
    }

    public void bubbleSort(int[] ans, Comparator c) {
        for (int i = 0; i < ans.length - 1; i++) {
            for (int j = 0; j < ans.length - i - 1; j++) {
                if (c.compare(ans[j], ans[j + 1]) > 0) {
                    int temp = ans[j];
                    ans[j] = ans[j + 1];
                    ans[j + 1] = temp;
                }
            }
        }
    }
    public static interface Comparator {
        public int compare (Object o1, Object o2) ;
    }
}
  • 写回答

3条回答 默认 最新

  • Huazie 优质创作者: 编程框架技术领域 2023-06-22 18:03
    关注

    你自己定义了一个 Comparator 接口,这肯定是不行的, Arrays.sort 里面引入的是 java.util.Comparator

            Integer[] ans = {3, 4, 5, 7, 8, 10};
            Arrays.sort(ans, new Comparator<Integer>(){
                @Override
                public int compare(Integer i1, Integer i2) {
                    return i1 - i2;
                }
            });
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 6月30日
  • 已采纳回答 6月22日
  • 创建了问题 6月22日