简体   繁体   English

线程“主”java.lang.ClassCastException 中的异常:proj.Car 无法转换为 java.lang.Comparable

[英]Exception in thread "main" java.lang.ClassCastException: proj.Car cannot be cast to java.lang.Comparable

I'm trying create something like this tutorial on creating a graph and I'm coming across an error: Exception in thread "main" java.lang.ClassCastException: proj.Car cannot be cast to java.lang.Comparable .我正在尝试创建类似于创建图形的教程的内容,但遇到了一个错误: Exception in thread "main" java.lang.ClassCastException: proj.Car cannot be cast to java.lang.Comparable

Car class汽车class

 Integer id;
 String model;
 String brand;
 float price;

Cars class (both have been instantiated already):汽车 class (两者都已实例化):

TreeMap<Integer, Car> m;
TreeMap<Car, LinkedList<Car>> adjList;

Main:主要的:

for(Map.Entry<Integer, Car> entry : a.m.entrySet()){
             Car val = entry.getValue();
             LinkedList<Car> list = new LinkedList<Car>();
             a.adjList.put(val, list);
}

This creates an adjList for every Car object in m but the problem is the error.这将为m中的每辆汽车 object 创建一个adjList ,但问题是错误。 I've tried making sure the entered variables are of the same type by using instanceof and it returns true.我尝试使用instanceof确保输入的变量属于同一类型,并且它返回 true。 So i'm not sure where the error comes from.所以我不确定错误来自哪里。

TreeMap is a SortedMap implementation and it requires its keys to implement the Comparable interface in order to sort the keys. TreeMap 是一个 SortedMap 实现,它需要它的键来实现 Comparable 接口以便对键进行排序。

So I assume Car does not implement Comparable.所以我假设 Car 没有实现 Comparable。 If you add this implementation, you'll be fine.如果你添加这个实现,你会没事的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程“主”java.lang.ClassCastException 中的异常:setcollection.Enseignant 无法转换为 java.lang.Comparable - Exception in thread “main” java.lang.ClassCastException: setcollection.Enseignant cannot be cast to java.lang.Comparable 错误:“线程中的异常”main“java.lang.ClassCastException:manycard.Main$Card 无法转换为 java.lang.Comparable” - Error: “Exception in thread ”main“ java.lang.ClassCastException: manycard.Main$Card cannot be cast to java.lang.Comparable” 线程“主”java.lang.ClassCastException 中的异常:javafx.util.Pair 无法转换为 java.lang.Comparable - Exception in thread “main” java.lang.ClassCastException: javafx.util.Pair cannot be cast to java.lang.Comparable java.lang.ClassCastException: 不能转换为 java.lang.Comparable - java.lang.ClassCastException: cannot be cast to java.lang.Comparable Java 运行时错误:java.lang.ClassCastException:main.Scrapper$aktie 无法转换为 java.lang.Comparable - Java runtime error: java.lang.ClassCastException: main.Scrapper$aktie cannot be cast to java.lang.Comparable java.lang.ClassCastException:尝试排序List时,java.util.LinkedHashMap无法转换为java.lang.Comparable异常 - java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to java.lang.Comparable exception when trying sort a List java.lang.ClassCastException:java.util.HashMap无法强制转换为java.lang.Comparable - java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.Comparable java.lang.ClassCastException:class java.util.TreeMap 无法转换为 class java.lang.Comparable - java.lang.ClassCastException: class java.util.TreeMap cannot be cast to class java.lang.Comparable java.lang.ClassCastException:com.MyComp.model.Image无法转换为java.lang.Comparable - java.lang.ClassCastException: com.MyComp.model.Image cannot be cast to java.lang.Comparable java.lang.ClassCastException:jdk.nashorn.internal.objects.NativeArray无法转换为java.lang.Comparable - java.lang.ClassCastException: jdk.nashorn.internal.objects.NativeArray cannot be cast to java.lang.Comparable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM