Java 中的 ClassCastException

发布于 2024-10-21 07:42:52 字数 805 浏览 1 评论 0原文

我在下面的代码中遇到了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酒与心事 2024-10-28 07:42:52

正如 compareTo javadoc 所述:

ClassCastException - 如果指定对象的类型阻止它与此对象进行比较。

看来您的 targetString 类型,即 Comparablecurrent.getValue()返回Integer类型的对象。

您可以尝试执行 String.valueOf(current.getValue) 如果它工作正常那么我是对的。 :)

As compareTo javadoc states:

ClassCastException - if the specified object's type prevents it from being compared to this object.

It seems that your target is of a String type which is Comparable<String> and current.getValue() returns an object of Integer type.

You can try to do String.valueOf(current.getValue) if it works ok then I'm right. :)

挽梦忆笙歌 2024-10-28 07:42:52
private ToBeFound find(TreeNode current, Comparable targetObject)

看起来 current 是一个 TreeNode...

private ToBeFound find(TreeNode current, Comparable targetObject)

Looks like current is a TreeNode...

幸福还没到 2024-10-28 07:42:52
System.out.println(target.compareTo(current.getValue()));

将原始 int 转换为 String

System.out.println(Integer.toString(target.compareTo(current.getValue())));

compareTo 的返回类型是 int,因此您可以使用What is the type of target? ?看起来您正在将字符串与整数进行比较。

System.out.println(target.compareTo(current.getValue()));

Return type of compareTo is int, so you can convert your primitive int to String using

System.out.println(Integer.toString(target.compareTo(current.getValue())));

What is the type of target? Seems like you are comparing String against Integer.

淡忘如思 2024-10-28 07:42:52

如果您的示例代码反映了您实际运行的内容,则:

System.out.println(target.compareTo(current.getValue()));
  1. 您的代码未显示 target 的声明,但异常消息暗示它必须是<代码>字符串。 (您显示使用 targetObject 的声明...但这不是代码实际使用的!!)

  2. 您的代码没有显示 ToBeFound.getValue( ) 方法...但异常消息暗示它必须Integer

  3. 如果参数对象不是字符串,String.compareTo(Object) 会抛出 CCE。

If your sample code is reflects what you are actually running then:

System.out.println(target.compareTo(current.getValue()));
  1. Your code does not show the declaration for target, but the exception message implies that it must be String. (You show use the declaration for targetObject ... but that's not what the code is actually using!!)

  2. Your code doesn't show the declaration for the ToBeFound.getValue() method ... but the exception message implies that it must be Integer.

  3. The String.compareTo(Object) throws a CCE if the parameter object is not a String.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文