【问题标题】:collections.sort not sorting, get output unsorted onlycollections.sort 不排序,仅获取未排序的输出
【发布时间】:2019-02-02 00:39:25
【问题描述】:

我的 Java 程序没有对输出进行排序。我没有使用 Java 的经验,在查看了类似的问题后,我无法找出我的代码存在的问题。

输出显示 3 个球体及其颜色,但不显示半径或按其区域排序。

以下是我的程序中涉及的 3 个 .java 文件,我在 Eclipse 中没有错误或警告,所以我假设我将一些参数或值放在了错误的位置...非常感谢任何帮助!

ComparableSphere.java

public class ComparableSphere extends GeometricObject implements Comparable<ComparableSphere>{

private double radius;

public ComparableSphere(){
    this("white",0);
    this.radius = 0;
}
public ComparableSphere(String color, double radius){
    super(color);
    this.radius = radius;
}
public double area() {
    return Math.PI * this.radius * this.radius * 4;
}
public double perimeter() {
    return 2 * Math.PI * this.radius;
}

@Override
public int compareTo(ComparableSphere o) {
    // TODO Auto-generated method stub
    return 0;
}

}

GeometricObject.java

public abstract class GeometricObject {

private String color;


protected GeometricObject(){
    this("white");
}


protected GeometricObject(String color) {
    this.color = color;
}


public String getColor(){
    return this.color;
}


public void setColor(String color){
    this.color = color;
}


public abstract double area();


public abstract double perimeter();


public String toString() {
    return this.getClass().getSimpleName() + ": color= " + this.color;
}



public boolean equals(Object obj) {
    if(!(obj instanceof GeometricObject)){
        return false;
    }

    GeometricObject other = (GeometricObject)obj;
    return this.color.equalsIgnoreCase(other.color);
}

}

driver.java

import java.util.ArrayList;
import java.util.Collections;

public class driver {
    public static void main(String[] args){
        ComparableSphere sphere1 = new ComparableSphere("Purple", 10.1);
        ComparableSphere sphere2 = new ComparableSphere("Orange", 3.8);
        ComparableSphere sphere3 = new ComparableSphere("Tan", 5.2);

        ArrayList<ComparableSphere> sphereList = new ArrayList<ComparableSphere>();

        sphereList.add(sphere1);
        sphereList.add(sphere2);
        sphereList.add(sphere3);

        System.out.println("Unsorted list: \n"+sphereList+"\n");

        Collections.sort(sphereList);
        System.out.println("Sorted list: \n"+sphereList);

}

}

enter image description here

【问题讨论】:

  • 你好像没有写compareTo方法。当您的程序无法比较两个对象时,您希望如何对对象进行排序?
  • public int compareTo(ComparableSphere o) { return 0; } 表示所有对象都比较 相等 (因为这就是返回值 0 的意思),如果它们都相等,你为什么会期望 sort()什么都做? --- 实现compareTo方法。
  • 写在哪里? compareTo 方法在 ComparableSphere.java 中,是否也需要在 driver.java 中?

标签: java arrays collections compareto


【解决方案1】:

根据the compareTo contract,您的compareTo 始终返回0,这意味着您正在考虑所有ComparableSphere 对象彼此相等:

将此对象与指定对象进行比较以进行排序。返回负整数、零或正整数,因为此对象小于、等于或大于指定对象。

这意味着Collections.sort认为它无关紧要,因为所有的对象都是平等的。

你需要在compareTo方法中编写逻辑,来比较这个对象和传入的对象,并返回一个合适的值。这将为Collections.sort 提供正确排序列表所需的信息。

【讨论】:

  • 我明白我在这里做错了什么。我尝试添加:return this.radius.compareTo(o.radius); 但我现在收到错误无法在原始类型 double 上调用 compareTo(double)
  • 环顾四周后,我发现我不能在 double 类型上使用 compareTo。我是否需要重写我的代码才能使用 compareTo 或者是否有其他方法可以使用我拥有的代码来实现 compareTo?
  • 使用Double.compare
猜你喜欢
  • 2018-10-11
  • 2019-03-06
  • 1970-01-01
  • 1970-01-01
  • 2019-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多