是什么导致Java类强制转换异常错误?

我的教授正在让我们实现Java可比较的接口,以帮助我的二进制搜索树比较包含被扫描单词的单词对象,然后将该单词与其他单词进行比较,但是由于某种原因,我正在做的第二个操作抛出了错误。我确实知道他给我们使用的方法确实在其中强制转换了(TreeComparable),但我仍然不是100%知道为什么这是编译器冻结的行并给我错误

java.lang.ClassCastException: class ObjectTreeNode cannot be cast to class 
TreeComparable (ObjectTreeNode and TreeComparable are in unnamed module of loader java.net.URLClassLoader

这就是导致它的方法

public void insertBSTDup(Object o) {
        ObjectTreeNode p, q;

        ObjectTreeNode r = new ObjectTreeNode(o);
        if (root == null)
            root = r;
        else {
            p = root;
            q = root;
            while (q != null && ((TreeComparable)(r.getInfo())).compareTo(p.getInfo()) != 0) { <---------
                p = q;
                if (((TreeComparable)(r.getInfo())).compareTo(p.getInfo()) < 0)
                    q = p.getLeft();
                else
                    q = p.getRight();
            }
            if (((TreeComparable)(r.getInfo())).compareTo(p.getInfo()) < 0)
                setLeftChild(p, r);
            else if (((TreeComparable)(r.getInfo())).compareTo(p.getInfo()) > 0)
                setRightChild(p, r);
            else ((TreeComparable)(p.getInfo())).operate(r.getInfo());
        }
    }

到目前为止,我的Word类是这样,底部是compareTo方法,其实现方式与我在第一次分配中所做的第一个compareTo相似,并且它正在比较字串,以便知道将其添加到何处。

public class Word implements TreeComparable
{
    private String word;
    private ObjectList list = new ObjectList();
    private ObjectListNode obj;
    private int numberOfTimes = 1, LineNumber, position;


    public Word(String word, int LineNumber, int position)
    {
        this.word = word;
        this.LineNumber = LineNumber;
        this.position = position;
    }

    public int compareTo(Object o)
    {
        Word w = (Word) o;
        return word.compareTo(w.getWord());
    }

    public String getWord()
    {
        return word;
    }

这是treeComparable方法

public interface TreeComparable
{
    public int compareTo(Object O);
    public void operate(Object O);
    public void visit();
}

除了删除treeComparable强制转换之外,我还需要做些什么改变以使错误消失,而我必须继续进行此项目

另外,这是Word对象将要进入的objectTreeNode对象

public class ObjectTreeNode implements ObjectTreeNodeInterface
{
    private Object info;
    private ObjectTreeNode left;
    private ObjectTreeNode right;

    public ObjectTreeNode() {
        info = null;
        left = null;
        right = null;
    }

    public ObjectTreeNode (Object o) {
        info = o;
        left = null;
        right = null;
    }

    public void setInfo(Object o) {
        info = o;
    }

    public Object getInfo() {
        return info;
    }

    public void setLeft(ObjectTreeNode p) {
        left = p;
    }

    public ObjectTreeNode getLeft() {
        return left;
    }

    public void setRight(ObjectTreeNode p) {
        right = p;
    }

    public ObjectTreeNode getRight() {
        return right;
    }
}

这是主要的

public static void main(String args[]) throws IOException
    {
        Hash h = new Hash();
        Word w;
        ObjectBinaryTree bt = new ObjectBinaryTree();
        ObjectTreeNode node;
        Scanner in = new Scanner(new File("getty.txt"));
        PrintWriter pw = new PrintWriter(new FileWriter("csis.txt"));
        int numberOfLines = 1;
        //h.check();


        while(in.hasNext())
        {
            String word = in.nextLine();
            String[] ar = word.split(" ", 0);

            System.out.print("\n" + (numberOfLines++) + ": ");
            int i = 0;
            while(i < ar.length)
            {
                char check = ar[i].charAt(ar[i].length() - 1);
                if(check == ',' || check == '.' || check == '!' || check == '?')
                {
                    ar[i] = ar[i].substring(0, ar[i].length() - 1);
                }
                w = new Word(ar[i], numberOfLines, (i + 1));
                node = new ObjectTreeNode(w);
                //System.out.println(ar[i]);
                bt.insertBSTDup(node);
                System.out.print("(" + (i + 1) + ") " + ar[i] + " ");
                i++;
            }
        }
    }
评论