Java 中的 ClassCastException
我在下面的代码中遇到了 ClassCastException,这对我来说没有多大意义,因为 targetObject
是一个 Comparable,而 current
是一个 ToBeFound。整数和字符串从哪里来?
ToBeFound origin = new ToBeFound();
public ToBeFound findWrapper(Comparable targetObject)
{
return find(origin, targetObject);
}
private ToBeFound find(ToBeFound current, Comparable targetObject)
{
if (current == null)
{
return null;
}
System.out.println(targetObject.compareTo(current.getValue()));
// Exception in thread "main" java.lang.ClassCastException:
// java.lang.Integer cannot be cast to java.lang.String
return new ToBeFound();
}
//body of main() that calls the methods
master.find( (Comparable) scanner.next());
// Scanner is a java.util.Scanner scanning System.in
I'm getting a ClassCastException in the below code, which doesn't make much sense to me, as targetObject
is a Comparable and current
is a ToBeFound. Where are the Integer and String coming from?
ToBeFound origin = new ToBeFound();
public ToBeFound findWrapper(Comparable targetObject)
{
return find(origin, targetObject);
}
private ToBeFound find(ToBeFound current, Comparable targetObject)
{
if (current == null)
{
return null;
}
System.out.println(targetObject.compareTo(current.getValue()));
// Exception in thread "main" java.lang.ClassCastException:
// java.lang.Integer cannot be cast to java.lang.String
return new ToBeFound();
}
//body of main() that calls the methods
master.find( (Comparable) scanner.next());
// Scanner is a java.util.Scanner scanning System.in
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如
compareTo
javadoc 所述:看来您的
target
是String
类型,即Comparable
和current.getValue()
返回Integer
类型的对象。您可以尝试执行
String.valueOf(current.getValue)
如果它工作正常那么我是对的。 :)As
compareTo
javadoc states:It seems that your
target
is of aString
type which isComparable<String>
andcurrent.getValue()
returns an object ofInteger
type.You can try to do
String.valueOf(current.getValue)
if it works ok then I'm right. :)看起来
current
是一个TreeNode
...Looks like
current
is aTreeNode
...将原始 int 转换为 String
compareTo
的返回类型是 int,因此您可以使用What is the type of target? ?看起来您正在将字符串与整数进行比较。Return type of
compareTo
is int, so you can convert your primitive int to String usingWhat is the type of target? Seems like you are comparing String against Integer.
如果您的示例代码反映了您实际运行的内容,则:
您的代码未显示
target
的声明,但异常消息暗示它必须是<代码>字符串。 (您显示使用targetObject
的声明...但这不是代码实际使用的!!)您的代码没有显示
ToBeFound.getValue( )
方法...但异常消息暗示它必须是Integer
。如果参数对象不是字符串,
String.compareTo(Object)
会抛出 CCE。If your sample code is reflects what you are actually running then:
Your code does not show the declaration for
target
, but the exception message implies that it must beString
. (You show use the declaration fortargetObject
... but that's not what the code is actually using!!)Your code doesn't show the declaration for the
ToBeFound.getValue()
method ... but the exception message implies that it must beInteger
.The
String.compareTo(Object)
throws a CCE if the parameter object is not a String.