MyClass 不能转换为 java.lang.Comparable: java.lang.ClassCastException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19716790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MyClass cannot be cast to java.lang.Comparable: java.lang.ClassCastException
提问by user2234225
I am doing a java project and I got this problem and don't know how to fix it.
我正在做一个java项目,我遇到了这个问题,不知道如何解决。
The classes in my project (simplified):
我的项目中的类(简化):
public class Item {
private String itemID;
private Integer price;
public Integer getPrice() {
return this.price;
}
}
public class Store {
private String storeID;
private String address;
}
public class Stock {
private Item item;
private Store store;
private Integer itemCount;
public Integer getInventoryValue() {
return this.item.getPrice() * this.itemCount;
}
}
Then I try to sort an ArrayList
of Stock so I create another class called CompareByValue
然后我尝试对一个ArrayList
Stock进行排序,所以我创建了另一个名为CompareByValue
public class CompareByValue implements Comparator<Stock> {
@Override
public int compare(Stock stock1, Stock stock2) {
return (stock1.getInventoryValue() - stock2.getInventoryValue());
}
}
}
When I try to run the program, it gives the error:
当我尝试运行该程序时,它给出了错误:
Exception in thread "main" java.lang.ClassCastException: Stock cannot be cast to java.lang.Comparable
线程“main”中的异常 java.lang.ClassCastException: Stock 无法转换为 java.lang.Comparable
Anyone know what's wrong?
有谁知道怎么了?
回答by óscar López
It's because Stock
isn't implementing Comparable
. Either implement it:
这是因为Stock
没有实现Comparable
. 要么实现它:
public class Stock implements Comparable<Stock> {
public int compareTo(Stock o) {
// ...
}
}
... Or pass an instance of CompareByValue
as parameter to the sort()
method:
... 或者将CompareByValue
as 参数的实例传递给sort()
方法:
Collections.sort(list, new CompareByValue());
回答by Steve Park
only from above code, it looks fine. are you sure the exception from above code? if you just put Stock object into any sorted collection, you will see such exception and need to implement Comparable interface.
仅从上面的代码来看,它看起来不错。你确定上面代码的异常吗?如果你只是将 Stock 对象放入任何排序集合,你会看到这样的异常,需要实现 Comparable 接口。
But for the case where just pass your custom Comparator, you don't need to make Stock to be a Comparable. it is same case like you give anonymous Comparator implementation.
但是对于只传递自定义 Comparator 的情况,您不需要使 Stock 成为 Comparable。就像您提供匿名 Comparator 实现一样。
set your comparator to sorted collection like TreeSet and then add the stock object into sorted collection, it should work without implemting Comparable interface.
将比较器设置为像 TreeSet 这样的排序集合,然后将股票对象添加到排序集合中,它应该可以在不实现 Comparable 接口的情况下工作。
回答by barkman345
public class Student implements Comparator<Student> {
private String name;
private String surname;
private int matpaz;
private int progrpaz;
private int anglupaz;
private double average;
@Override
public int compare(Student o1, Student o2) {
String v1 = o1.surname;
String v2 = o2.surname;
if ((v1.compareTo(v2)) >0) return 1;
else return 0;
}
public static void alphabetically (Student[] s)
{
Arrays.sort(s);
System.out.printf(Arrays.toString(s));
}
}