线程“main”中的异常java.lang.ClassCastException:

发布于 2024-09-24 06:49:57 字数 1745 浏览 0 评论 0原文

我一直在使用驱动程序来测试我的一种数据结构(二叉搜索树) 我遇到过这个问题。 -当我向 bst 中插入超过 2 个对象时会发生这种情况 -我想要做什么:我在树中插入 4 个对象,然后删除 2 个对象,然后打印出我的 find 方法,以便它显示是否找到我请求的对象。 例如:

BinarySearchTree2<Integer> theData1 = new BinarySearchTree2<Integer>();
     long start1 = System.currentTimeMillis();  
   theData1.insert(c1);
  theData1.insert(c2);
  theData1.insert(c3);
    theData1.delete(c2);
    System.out.println(theData1.find(c1));
    System.out.println(theData1.find(c2));
    System.out.println(theData1.find(c3));
    System.out.println(theData1.find(c4));

我在运行时收到此错误:

Exception in thread "main" java.lang.ClassCastException: TreeNode无法转换为java.lang.Comparable 在 BinarySearchTree2.delete(BinarySearchTree2.java:83) 在 Driver5.main(Driver5.java:36) 中

,它指向我的 bst 类中的删除方法,即:

public void delete(E item) {

        TreeNode<E> nd = root;

        while(nd != null && nd.getItem().compareTo(item) != 0)
        {
            if(nd.getItem().compareTo(item) < 0)
                nd = nd.getRight();

            else
                 nd = nd.getLeft();
        }

        if( nd.getLeft() == null && nd.getRight() == null)
        {
            nd = null;
        }

        else if(nd.getLeft() != null && nd.getRight() == null)

        {
            nd.setItem((E)nd.getLeft());

        }
        else if(nd.getLeft() == null && nd.getRight() != null)
        {    
            nd.setItem((E)nd.getRight());

        }

        else if(nd.getLeft() != null && nd.getRight() != null)
        {

            nd.setItem((E)findsucc(nd));
        }    

}

错误直接指向我的删除方法中的这一行:

nd.setItem((E)nd.getRight());

I have been using a driver to test one of my data structures(Binary Search Tree)
and I have come across this issue.
-It happens when I insert more than 2 objects into the bst
-What I am trying to do: I am inserting 4 objects into the tree, then I am deleting 2 objects, and then printing out my find method so that it displays whether or not it found the objects I request.
for instance:

BinarySearchTree2<Integer> theData1 = new BinarySearchTree2<Integer>();
     long start1 = System.currentTimeMillis();  
   theData1.insert(c1);
  theData1.insert(c2);
  theData1.insert(c3);
    theData1.delete(c2);
    System.out.println(theData1.find(c1));
    System.out.println(theData1.find(c2));
    System.out.println(theData1.find(c3));
    System.out.println(theData1.find(c4));

I receive this error when i run it:

Exception in thread "main" java.lang.ClassCastException: TreeNode cannot be cast to java.lang.Comparable
at BinarySearchTree2.delete(BinarySearchTree2.java:83)
at Driver5.main(Driver5.java:36)

which then points to the delete method in my bst class which is:

public void delete(E item) {

        TreeNode<E> nd = root;

        while(nd != null && nd.getItem().compareTo(item) != 0)
        {
            if(nd.getItem().compareTo(item) < 0)
                nd = nd.getRight();

            else
                 nd = nd.getLeft();
        }

        if( nd.getLeft() == null && nd.getRight() == null)
        {
            nd = null;
        }

        else if(nd.getLeft() != null && nd.getRight() == null)

        {
            nd.setItem((E)nd.getLeft());

        }
        else if(nd.getLeft() == null && nd.getRight() != null)
        {    
            nd.setItem((E)nd.getRight());

        }

        else if(nd.getLeft() != null && nd.getRight() != null)
        {

            nd.setItem((E)findsucc(nd));
        }    

}

the error points directly to this line in my delete method:

nd.setItem((E)nd.getRight());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

笑脸一如从前 2024-10-01 06:49:57

我猜你的 E 声明是“E extends Comaprable”,在这种情况下,当你调用 nd.getRight 时,它返回 TreeNode 实例,该实例必须具有可比性才能成功。

发生异常的行应如下所示,以便强制转换通过

nd.setItem(nd.getRight.getItem)

I guess your declaration of E is "E extends Comaprable", in that case when you called nd.getRight it returned TreeNode instance which has to be comparable for the cast to succeed.

The line where the exception occured should look like below for the cast to pass

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