【发布时间】:2014-02-28 18:17:43
【问题描述】:
我收到此错误java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.Comparable
这是有问题的代码。
final String[] methods = parseRule.getMethods();
// add to the param.
methodsToInsert.add(methods); // <-- error from here
//Where
public String[] getMethods() {
return new String[]{new String(parseMethod), new String(unParseMethod)};
}
//and param
Queue<String[]> methodsToInsert
//and at some point the methodsToInsert Object is
new PriorityQueue<String[]>();
我不完全确定为什么?任何建议
谢谢
哇,谢谢大家,我认为在以前的重构队列中是需要的,但随着它的发展,它不再需要,感谢您提供信息以及如何使用比较器进行修复。由于不再需要 PriorityQueue,我将返回 List。
【问题讨论】:
-
是因为我正在使用 add 吗?不放?
-
不,阅读错误信息。
add接受(对于该队列接受)有一个(运行时)类型限制,即它们必须实现 Comparable。 -
@AnotherCompilerError 没错。没有数组实现 Comparable。
-
你的队列具体是什么样的队列?你能发布一个可编译的例子吗?
-
您可以使用带有
Comparator的constructor;你需要写一个String[]比较器。
标签: java queue priority-queue