链表程序不会删除对象,并且由于未知原因抛出强制转换异常?



我正在写一个基本的链表程序。为了测试它,我只是创建了一个基本的列表,添加了一堆东西,然后试图删除它们。它抛出了一个错误,我不明白。

程序:

public static void main(String[] args) {
    OrderedLinkedList one = new OrderedLinkedList();
    for(int i = 0; i<=20; i=i+2){
        one.add(i);
    }
    one.remove(3);
    //System.out.println(one.toString());
}

我想看看如果我试图移除一个不存在的对象会发生什么。但是当我这样做的时候,它抛出了这个异常:

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

异常似乎发生在包含compareTo()方法的行中的我的remove方法中。我如何尝试将整型转换为字符串?当它传递给方法时,3不应该是一个可比性吗?

public boolean add(Comparable obj)
    {
        if(head.next == null){
        OrderedListNode newNode = new OrderedListNode(obj, head, tail);
        }
        else{
            OrderedListNode newNode = new OrderedListNode(obj,tail.previous,tail);
            tail.previous.next = newNode;
            tail.previous = newNode;
        }
        return true;
    }
    public boolean remove(Comparable obj)
    {
        OrderedListNode temp = head;
        while(temp != null){
            if(temp.theItem.compareTo(obj) == 0){
                temp.previous.next = temp.next;
                temp.next.previous = temp.previous;
            }
            else{
                temp = temp.next;
            }
        }
        return true;
    }
/**
 *  Empty this OrderedLinkedList.
 */
public void clear()
{
    // reset header node
    head = new OrderedListNode("HEAD", null, null);
    // reset tail node
    tail = new OrderedListNode("TAIL", head, null);
    // header references tail in an empty LinkedList
    head.next = tail;
    // reset size to 0
    theSize = 0;

}
private class OrderedListNode{
    Comparable theItem;
    OrderedListNode next;
    OrderedListNode previous;
    public OrderedListNode(Comparable theItem, OrderedListNode previous, OrderedListNode next){
        this.theItem = theItem;
        this.next = next;
        this.previous=previous;
    }
}
@Override
public String toString()
{
    String s = "";
    OrderedListNode currentNode = head.next;
    while (currentNode != tail)
    {
        s += currentNode.theItem.toString();
        if (currentNode.next != tail)
        {
            s += ", ";
        }
        currentNode = currentNode.next;
    }
    return s;
}

您的问题在于清洁方法。它正在实例化字符串数据类型节点,并将其放入尾部和头部。所以当remove()被调用时,当它在你的对象列表中迭代/循环时,当它到达你的字符串类型节点时,异常被抛出,因为它试图比较int和string。

无论如何,我已经修复了你的问题通过重写部分代码。它不是完美的,但至少它不再触及问题

变化如下。

第一个:在remove方法中-你需要一个break语句,否则代码会进入无限循环。

我已经重做了你的Clean方法,因为它包含一些逻辑错误。基本上,您将希望在清理期间将事物设置为null或0,而不是为它初始化新的尾部和头部对象。

第三:从逻辑上讲,add逻辑也是不正确的。我也做了重做。

注意:由于时间限制,我没有真正测试我自己的更改,所以可能仍然存在错误,这不在这个问题的范围内。

如果你发现任何错误,请留言回复,我会找时间再检查一次的。

public class OrderedLinkedList {
    OrderedListNode head = null;
    OrderedListNode tail = null;
    int theSize = 0;
    public boolean add(Comparable obj)
    {
        if(head == null){
            head = new OrderedListNode(obj, head, tail);
        }
        else{
            OrderedListNode newNode = new OrderedListNode(obj, /*prev=*/tail, /*next=*/null);
            if (tail == null)
                head.next = newNode;
            else {
                tail.next = newNode;
                // tail.previous = newNode; No need this line. Already set in constructor
            }
            tail = newNode;
        }
        return true;
    }
    public boolean remove(Comparable obj)
    {
        OrderedListNode temp = head;
        boolean elemRemoved = false;
        while(temp != null){
            if(temp.theItem.compareTo(obj) == 0) {
                // dereference this object for garbage collection
                if (temp.previous != null)
                    // Not a head - Head will not have previous
                    temp.previous.next = temp.next;
                temp.next.previous = temp.previous;
                elemRemoved = true;
                break;
            }
            else{
                temp = temp.next;
            }
        }
        return elemRemoved;
    }
    /**
     *  Empty this OrderedLinkedList.
     */
    public void clear()
    {
        // reset header node
        head = null;
        // reset tail node
        tail = null;
        // header references tail in an empty LinkedList
        //head.next = tail;
        // reset size to 0
        theSize = 0;

    }
    private class OrderedListNode{
        Comparable theItem;
        OrderedListNode next;
        OrderedListNode previous;
        public OrderedListNode(Comparable theItem, OrderedListNode previous, OrderedListNode next){
            this.theItem = theItem;
            this.next = next;
            this.previous=previous;
        }
    }
    @Override
    public String toString()
    {
        String s = "";
        OrderedListNode currentNode = head.next;
        while (currentNode != tail)
        {
            s += currentNode.theItem.toString();
            if (currentNode.next != tail)
            {
                s += ", ";
            }
            currentNode = currentNode.next;
        }
        return s;
    }
}

public class main {

    public static void main(String[] args) {
        OrderedLinkedList one = new OrderedLinkedList();
        one.clear();
        for(int i = 0; i<=20; i=i+2){
            one.add(i);
        }
        boolean removedOK = one.remove(3);
        System.out.println("Attempt 1 : Something was removed => " + removedOK);
        removedOK = one.remove(2);
        System.out.println("Attempt 2: Something was removed => " + removedOK);
        removedOK = one.remove(4);
        System.out.println("Attempt 3: Something was removed => " + removedOK);
    }
}

相关内容

  • 没有找到相关文章

最新更新