Java collections.sort不排序,仅获取未排序的输出
我的Java程序没有对输出进行排序。我没有Java方面的经验,在研究了类似的问题之后,我无法用我的代码找出问题所在 输出显示3个球体及其颜色,但不显示半径或按其面积进行排序 下面是我的程序中涉及的3.java文件,我在Eclipse中没有错误或警告,所以我假设我把一些参数或值放错了位置。。。非常感谢您的帮助,非常感谢 javaJava collections.sort不排序,仅获取未排序的输出,java,arrays,collections,compareto,Java,Arrays,Collections,Compareto,我的Java程序没有对输出进行排序。我没有Java方面的经验,在研究了类似的问题之后,我无法用我的代码找出问题所在 输出显示3个球体及其颜色,但不显示半径或按其面积进行排序 下面是我的程序中涉及的3.java文件,我在Eclipse中没有错误或警告,所以我假设我把一些参数或值放错了位置。。。非常感谢您的帮助,非常感谢 java public class ComparableSphere extends GeometricObject implements Comparable<Compar
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;
}
}
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);
}
import java.util.ArrayList;
导入java.util.Collections;
公务舱司机{
公共静态void main(字符串[]args){
ComparableSphere1=新的ComparableSphere(“紫色”,10.1);
ComparableSphere2=新的ComparableSphere(“橙色”,3.8);
可比球体3=新的可比球体(“Tan”,5.2);
ArrayList sphereList=新的ArrayList();
sphereList.add(sphere1);
sphereList.add(sphere2);
sphereList.add(sphere3);
System.out.println(“未排序的列表:\n”+sphereList+“\n”);
集合。排序(sphereList);
System.out.println(“排序列表:\n”+sphereList);
}
}
您的
compareTo
始终返回0
,这意味着您考虑的所有可比球体
对象都是相等的,根据:
将此对象与订单的指定对象进行比较。当此对象小于、等于或大于指定对象时,返回负整数、零或正整数
这意味着Collections.sort
认为它与此无关,因为所有对象都是相等的
您需要在
compareTo
方法中写入逻辑,以便在此对象和传入对象之间进行比较,并返回适当的值。这将为集合提供正确排序所需的信息。您似乎没有编写比较方法。当您的程序无法比较两个对象时,您希望如何对对象进行排序?public int compareTo(ComparableSphere o){return 0;}
意味着对所有对象进行比较是相等的(因为这是返回值0的意思),如果它们都相等,您为什么希望sort()
要做什么吗?--实现compareTo
方法。将其写入何处?compareTo方法位于ComparableSphere.java中,它是否也需要位于driver.java中的某个位置?我知道我在这里做错了什么。我尝试添加:返回this.radius.compareTo(o.radius)代码>但我现在遇到一个错误,无法在基本类型double上调用compareTo(double)。环顾四周后,我发现我无法在double类型上使用compareTo。我是否需要重新编写代码以使用compareTo,或者是否有其他方法用我拥有的代码实现compareTo?请使用。
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);
}