简体   繁体   English

在Java中使用泛型时生成的类强制转换异常

[英]Class Cast Exception generated while using generics in Java

I am working on a homework assignment and I cannot figure out why I keep getting a class cast exception whenever I try to run my code. 我正在做一项家庭作业,我想不出为什么每次尝试运行代码时都会不断收到类强制转换异常。 I think it is due to the (path.get(i)) but I can not seem to figure out a way to fix it. 我认为这是由于(path.get(i))造成的,但我似乎无法找出一种解决方法。 The error I receive is 我收到的错误是

 Exception in thread "main" java.lang.ClassCastException: avltreend.BinarySearchTreeND$BSTNodeND cannot be cast to avltreend.AVLtreeND$AVLTreeNode
    at avltreend.AVLtreeND.balancePath(AVLtreeND.java:64)
    at avltreend.AVLtreeND.insert(AVLtreeND.java:27)
    at avltreend.AVLtreeND.TestAVL(AVLtreeND.java:233)
    at avltreend.AVLtreeND.main(AVLtreeND.java:244)
Java Result: 1

Some of the code is below 下面的一些代码

   private void balancePath(K d) {
    ArrayList<BSTNodeND<K>> path = path(d);
    for (int i = path.size() - 1; i>= 0; i--) {
     //   System.out.println(path);
        AVLTreeNode<K> A = (AVLTreeNode<K>)(path.get(i));
        findheight(A);
        AVLTreeNode<K> POA = (A == root) ? null :
                (AVLTreeNode<K>)(path.get(i - 1));

The error seems to appear when the 5th line above is run. 运行上面的第5行时,似乎会出现错误。

      class BSTNodeND < L extends Comparable< ? super L > > {
     L data;
     BSTNodeND < L > left, right, parent;

     BSTNodeND (L d)                  {data = d;}
     BSTNodeND (L d, BSTNodeND <L> p) {data = d; parent = p;}

     public String toString () {
        return data.toString();} // end toString method
  } 


   protected class AVLTreeNode<L extends Comparable<? super L>>
      extends BSTNodeND<L> {
    protected int height = 0; // New data field

    public AVLTreeNode(L d) {
      super(d);
    }

And that is the AVLTreeNode class. 那就是AVLTreeNode类。

I cannot figure out why these two classes are not working together as I have altered a working example and it should work. 我无法弄清楚为什么这两个类不能一起工作,因为我已经更改了一个工作示例并且它应该工作。 Thanks for any help you can provide. 感谢您的任何帮助,您可以提供。

If the true runtime type of an object is A, then you cannot cast it to a subclass B. For example, 如果对象的真实运行时类型为A,则不能将其强制转换为子类B。例如,

class A {...}

class B extends A {
    public void announce() { 
        System.out.println("Hi, I'm an instance of B."); 
     }
}

public class Main {
   public static void main(String[] args) {
       A a = new A();
       A b = new B();
       ((B) b).announce(); // Valid
       ((B) a).announce(); // Exception!
   }
}

This is exactly what you are doing, casting an instance of BSTNodeND to its subclass, AVLTreeNode . 这正是您正在做的事情,将BSTNodeND的实例BSTNodeND转换为其子类AVLTreeNode

Your variable path is an ArrayList of BSTNodeND . 您的变量pathBSTNodeNDArrayList When you get an element you are trying to cast it into an AVLTreeNode . 当您获得一个元素时,您尝试将其转换为AVLTreeNode That is you are trying to cast the parent class into the child class wich might works if you have only "child" elements in your list but if the type of the object is BSTNodeND then you cannot cast it to its specialized subclass AVLTreeNode . 那就是您试图将父类转换为子类,如果列表中仅包含“子”元素,则可能BSTNodeND ,但是如果对象的类型为BSTNodeND则无法将其转换为其专门的子类AVLTreeNode

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM