无法强制转换为java.lang.Comparable [英] cannot be cast to java.lang.Comparable

查看:644
本文介绍了无法强制转换为java.lang.Comparable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.util.HashMap;
import java.io.*;
import java.util.*;
public class Fegan implements Comparable{
HashMap<String, Integer> cart = new HashMap<String, Integer>();
List list = new ArrayList<FoodItems>();
int x =0;
public void addToCart(FoodItems f)
{
    cart.put(f.name, f.valueOfFood);
}
public String display(FoodItems f)
{
    return(f.name + " costs " + f.valueOfFood);
}
public void addToList(FoodItems f)
{
    //FoodItems temp = (FoodItems) f;
    list.add(f);
}
public int compareTo(Object o)
{
    //FoodItems temp = (FoodItems) o;
    if(this.x == ((FoodItems)o).valueOfFood)
        return 0;
    else if (this.x <((FoodItems)o).valueOfFood)
        return 1;
    else 
        return -1;
}
public void sortMap(List list)
{
    for(int i =0; i< list.size(); i++)
    {
        FoodItems temp = (FoodItems) list.get(i);
        cart.put(temp.name, temp.valueOfItem);

    }
}
}







import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.io.*;
import java.util.*;
public class Test {
public static void main(String [] args)
{
    HashMap<String, Integer> cart = new HashMap<String, Integer>();
    FoodItems firts = new FoodItems("Chocolate" , 50);
    FoodItems second = new FoodItems("Juice", 79);
    FoodItems third = new FoodItems("Apple", 200);
    FoodItems forth = new FoodItems("Orange", 300);
    FoodItems fifth = new FoodItems("Milk" , 400);
    ArrayList items = new ArrayList();
    items.add(firts);
    items.add(second);
    items.add(third);
    items.add(forth);
    items.add(fifth);
    Collections.sort(items);
    Iterator itr = items.iterator();
    Fegan myFegan = new Fegan();
    myFegan.sortMap(items);

    while(itr.hasNext()){
        Object element = itr.next();
        System.out.println(element + "\n");
    }
}
}

为什么写作

Exception in thread "main" java.lang.ClassCastException: FoodItems cannot be cast to java.lang.Comparable
at java.util.ComparableTimSort.countRunAndMakeAscending(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
at Test.main(Test.java:21)

提前谢谢!

推荐答案


  • 实现 Comparable 的对象是 Fegan

    • the object which implements Comparable is Fegan.
    • 方法 compareTo 你要覆盖它应该有 Fegan object作为参数,而你将它转换为 FoodItems 。你的 compareTo 实现应描述 Fegan 与另一个 Fegan 。

      The method compareTo you are overidding in it should have a Fegan object as a parameter whereas you are casting it to a FoodItems. Your compareTo implementation should describe how a Fegan compare to another Fegan.


      • 要实际进行排序,您可能需要制作 FoodItems 实现 Comparable 以及复制粘贴你的实际 compareTo 逻辑。

      • To actually do your sorting, you might want to make your FoodItems implement Comparable aswell and copy paste your actual compareTo logic in it.

      这篇关于无法强制转换为java.lang.Comparable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆