【发布时间】:2021-05-20 17:29:28
【问题描述】:
我正在尝试创建类似 this 的关于创建图表的教程,但我遇到了一个错误:Exception in thread "main" java.lang.ClassCastException: proj.Car cannot be cast to java.lang.Comparable。
汽车类
Integer id;
String model;
String brand;
float price;
Cars 类(都已实例化):
TreeMap<Integer, Car> m;
TreeMap<Car, LinkedList<Car>> adjList;
主要:
for(Map.Entry<Integer, Car> entry : a.m.entrySet()){
Car val = entry.getValue();
LinkedList<Car> list = new LinkedList<Car>();
a.adjList.put(val, list);
}
这会为m 中的每个 Car 对象创建一个 adjList,但问题是错误。我尝试使用instanceof 确保输入的变量属于同一类型,它返回true。所以我不确定错误来自哪里。
【问题讨论】:
-
车具可比.....请看stackoverflow.com/questions/44355842/…
-
你的 Car 类是否实现了 Comparable 接口?
-
@rr11 可以发一下
Car的定义吗