java.lang.ClassCastException:无法转换为java.lang.Compariable
在这个方法中,我想按升序对浮点值进行排序,为此我编写了类置信度比较器(参见下面的源代码) 在java.lang.ClassCastException:无法转换为java.lang.Compariable,java,android,comparator,comparable,Java,Android,Comparator,Comparable,在这个方法中,我想按升序对浮点值进行排序,为此我编写了类置信度比较器(参见下面的源代码) 在getResults()方法中的outputQueue中添加两个信任之后 我还尝试在Results类中实现Compariable接口,因为它按自然顺序对值进行排序: public class Result implements Comparable{ private String result; private float confidence; @Override public int compare
getResults()
方法中的outputQueue
中添加两个信任之后
我还尝试在Results类中实现Compariable接口,因为它按自然顺序对值进行排序:
public class Result implements Comparable{
private String result;
private float confidence;
@Override
public int compareTo(Object o) {
Result other = (Result) o;
return this.confidence.compareTo(other.confidence);
}
但它显示了错误
“无法将方法比较解析为(浮点)”
您不是在比较浮点
s,而是在比较结果
s和其中的浮点值。
所以应该是可比的
然后试着这样做,因为信心不是一个目标,在你的对比中:
return Float.compare(this.confidence, other.confidence);
使用完整的代码:
public class Result implements Comparable<Result> {
private String result;
private float confidence;
@Override
public int compareTo(Result other) {
return Float.compare(this.confidence, other.confidence);
}
}
公共类结果实现可比较{
私有字符串结果;
私人信心;
@凌驾
公共整数比较(结果其他){
返回浮点数。比较(这个。置信度,其他。置信度);
}
}
信心
是一个基本要素。原语没有方法。仅供参考:不要使用原始泛型。将实现可比较的
更改为实现可比较的
,而不是本.置信度。当然,比较到(其他.置信度)
。仅供参考:不要使用原始泛型。将所有PriorityQueue
更改为PriorityQueue
无论您选择哪种方式:不要使用原始泛型!!
public class Result implements Comparable{
private String result;
private float confidence;
@Override
public int compareTo(Object o) {
Result other = (Result) o;
return this.confidence.compareTo(other.confidence);
}
return Float.compare(this.confidence, other.confidence);
public class Result implements Comparable<Result> {
private String result;
private float confidence;
@Override
public int compareTo(Result other) {
return Float.compare(this.confidence, other.confidence);
}
}