gpt4 book ai didi

即使声明了可比较,Java Collections.sort() 也不起作用

转载 作者:行者123 更新时间:2023-12-01 17:53:01 25 4
gpt4 key购买 nike

这是我的接口(interface)类

public interface Thing {
int getVolume();
}

这是实现 Thing 的类

项目.java

public class Item implements Thing, Comparable<Thing> {
private String name;
private int volume;

public Item(String name,int volume){
this.name = name;
this.volume = volume;
}

@Override
public int getVolume() {
return this.volume;
}

public String getName(){
return this.name;
}

@Override
public String toString(){
return name+" ("+volume+" dm^3)";
}

// @Override
@Override
public int compareTo(Thing another) {
if(this.getVolume() < another.getVolume()){
return -1;
}

if(this.getVolume() == another.getVolume()){
return 0;
}
else{
return 1;
}
}

}

当我尝试使用以下命令运行主程序时,它运行良好//主程序.java

public class Main {

public static void main(String[] args) {
// test your program here
List<Item> items = new ArrayList<Item>();
items.add(new Item("passport", 2));
items.add(new Item("toothbrash", 1));
items.add(new Item("circular saw", 100));

Collections.sort(items);
System.out.println(items);



}
}

但是当我尝试在另一个实现 Thing 接口(interface)的类上运行 Collections.sort() 时,出现错误

这里是实现 Thing 接口(interface)的 box 类,当我尝试在 void sort() 函数中运行 Collections.sort(store) 时,它会给出错误,即使 store 是一个 List 并且 Box 类实现 Thing 接口(interface)我已经在 Item.java 类中为 Thing 定义了可比

Box.java

public class Box implements Thing {

private int maximumCapacity;
private List<Thing> store;

public Box(int maximumCapacity) {
this.maximumCapacity = maximumCapacity;
this.store = new ArrayList<Thing>();
}

public boolean addThing(Thing thing) {
// I.E. if the item added does not make the total volume go to max capacity only
// then add
if (this.getVolume() + thing.getVolume() < this.maximumCapacity) {
store.add(thing);
return true;
}
return false;
}

@Override
public int getVolume() {
// we calculate things of all items in the boxes (current value)
int currentWeight = 0;
for (Thing t : store) {
currentWeight += t.getVolume();
}
return currentWeight;
}

public List<Thing> getStore() {
return store;
}

public int numOfItems(){
return this.store.size();
}


public void sort(){

Collections.sort(store); // *****does not work ****//

}

}

It gives an error above for sort as "No suitable method found for sort(List <Thing>)."

我的问题是它是否可以在 main.java 程序中工作,其中项目以列表形式给出,那么为什么它不能在这里工作?如何解决?

最佳答案

它是您排序的主类 List<Item>哪里Item implements Thing, Comparable<Thing> .

Box您尝试排序的类 List<Thing> ,但是Thing本身没有实现Comparable<Thing> 。因此Java不知道如何排序Thing s。

要解决这个问题,您必须为两个 Thing 提供一个比较器s(由 Александр Нестеров 提议)或者您声明 Thing implements Comparable<Thing> :

public interface Thing extends Comparable<Thing>{

int getVolume();

//provide default method to sort any class which implements Thing
@Override
public default int compareTo(Thing another) {
return Integer.compare(this.getVolume(), another.getVolume());
}
}

关于即使声明了可比较,Java Collections.sort() 也不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47862572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com