You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java入门项目:按半径升序排序Circle数组与ArrayList报错求助

Java排序Circle列表和数组的问题解决与优化

问题1:Collections.sort(sortedList)报错

原因

Collections.sort(List<T>)要求泛型T必须实现Comparable<T>接口,否则无法确定元素的排序规则。你的Circle类未实现该接口,因此IDE提示方法不适用。

解决方法

有两种可选方案:

  • 方案1:修改Circle类实现Comparable接口
    让Circle类实现Comparable<Circle>,重写compareTo方法定义半径比较逻辑:

    public class Circle implements Comparable<Circle> {
        private double radius;
    
        public Circle(double radius) {
            this.radius = radius;
        }
    
        public double getRadius() {
            return radius;
        }
    
        @Override
        public int compareTo(Circle other) {
            // 按半径升序排序,降序则调换参数顺序
            return Double.compare(this.radius, other.radius);
        }
    }
    

    之后直接调用Collections.sort(sortedList)即可,同时要先把原列表元素复制到sortedList中:

    public void setSortedList() {
        sortedList.clear();
        sortedList.addAll(list);
        Collections.sort(sortedList);
    }
    
  • 方案2:使用Comparator比较器(无需修改Circle类)
    调用带比较器的排序方法,直接指定按半径排序:

    public void setSortedList() {
        sortedList.clear();
        sortedList.addAll(list);
        sortedList.sort(Comparator.comparingDouble(Circle::getRadius));
    }
    

问题2:自定义sortArray方法报错

错误原因

报错行 sortedArray[minI].getRadius()=sortedArray[i].getRadius()存在两个问题:

  1. getRadius()是返回值而非变量,不能作为赋值语句的左侧,因此提示“The left-hand side of an assignment must be a variable”;
  2. 内层循环结束后,i的值等于sortedArray.length,此时访问sortedArray[i]会触发数组越界异常;
  3. 排序逻辑错误:应该交换数组中Circle对象的位置,而非修改半径值(若Circle的半径是不可变属性,甚至无法修改)。

修正后的sortArray方法

private void sortArray() {
    int startScan;
    int minIndex;
    Circle minCircle;

    for (startScan = 0; startScan < sortedArray.length - 1; startScan++) {
        minIndex = startScan;
        minCircle = sortedArray[startScan];
        // 遍历找到当前区间内半径最小的Circle
        for (int i = startScan + 1; i < sortedArray.length; i++) {
            if (sortedArray[i].getRadius() < minCircle.getRadius()) {
                minCircle = sortedArray[i];
                minIndex = i;
            }
        }
        // 交换当前起始位置和最小元素的位置
        sortedArray[minIndex] = sortedArray[startScan];
        sortedArray[startScan] = minCircle;
    }
}

最优实现方式

Java提供了成熟的排序工具类,无需手动实现排序算法,效率更高且不易出错:

1. ArrayList排序

推荐使用列表自带的sort方法结合比较器,无需修改Circle类:

public void setSortedList() {
    sortedList.clear();
    sortedList.addAll(list);
    // 按半径升序排序,降序则加.reversed()
    sortedList.sort(Comparator.comparingDouble(Circle::getRadius));
}

2. 数组排序

使用Arrays.sort工具方法,同样支持两种方式:

  • 若Circle实现了Comparable接口:
    public void setSortedArray() {
        // 复制原数组,避免修改原数组顺序
        sortedArray = Arrays.copyOf(array, array.length);
        Arrays.sort(sortedArray);
    }
    
  • 若不修改Circle类:
    public void setSortedArray() {
        sortedArray = Arrays.copyOf(array, array.length);
        Arrays.sort(sortedArray, Comparator.comparingDouble(Circle::getRadius));
    }
    

内容的提问来源于stack exchange,提问作者Just a person

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 17:50:43