使用LLList

时间:2018-04-05 13:33:04

标签: java error-handling linked-list

我正在尝试编写从列表中删除所有项目的代码,但在尝试使用显示空指针异常的方法时出现错误。 哪条线导致此错误,我该如何解决?删除项后它也应该返回true,如果没有出现则返回false。

更新:错误是对象item2 = trav.item

public boolean removeAll(Object item){
    Node trav = head;
    while(trav != null){
        Object item2 = trav.item;
        // it could be `item2.equals` 
        if(item2.equals(item)){
            trav.next = trav;
            return true;
        }
        trav = trav.next; 
    }  
    return false;
}

3 个答案:

答案 0 :(得分:0)

除了我修理你的NPE。

如果您在修复删除代码时遇到问题,请查看此内容以供参考,它已经过测试。

我提供评论以解释发生的事情。

public class LList {

    // very basic singly linked node
    class Node {
        public Node next;
        public Object item;
        public Node(Object o) {
            item = o;
        }
    }

    // the head and tail of this list
    Node head = null;
    Node tail = null;

    public boolean removeAll(Object item) {
        int count = 0;
        if (head == null)
            return false;

        // while `head.item.equals(item)` remove the head
        while (head != null && item.equals(head.item)) {
            head = head.next;
            count++;
        }
        // don't need to worry about updating `head` anymore
        // because now `!head.item.equals(item)`, else `head == null`
        if (head == null)
            return count > 0;

        // previous and current nodes
        Node prev = head; // !head.item.equals(item)
        Node curr = head.next;
        while (curr != null) {
            if (item.equals(curr.item)) {
                // to remove `curr` make the previous node skip it
                // and link to the next node instead
                prev.next = curr.next;
                count++;
            } else {
                // if curr was removed then prev stays put
                // otherwise curr is the new prev
                prev = curr;
            }
            // always move curr forward
            curr = curr.next;
        }
        return count > 0;
    }

答案 1 :(得分:-1)

尝试按照代码的方式编写代码。虽然没有测试。它可能有所帮助,也可能没有。拿一粒盐。

public boolean removeAll(Object item) {
        int removeCounter = 0;
        if (head == null) {
            return false;
        } else if (head.value.equals(item)) {
            head = head.next;
            removeCounter++;
        }
        Node previousTrav = head;
        Node trav = head.next;
        while(trav.next != null) {
            if (trav.item.equals(item)) {
                previousTrav.next = trav.next;
                removeCounter++;
            }
            previousTrav = trav;
            trav = trav.next;
        }
        if(removeCounter > 0) {
            return true;
        }
        return false;
    }

答案 2 :(得分:-1)

我可以看到两个问题:

如果item2null,您将获得NullPointerException

trav.next = trav;产生了一个盘旋。在方法removeAll的下一次调用中,您将遇到无限循环。您需要将下一个节点设置为上一个节点以避免盘旋。

public boolean removeAll(Object item) {
    if (item == null} {
        return false;
    } else {
        Node node = head;
        Node prevNode = null;
        boolean result = false;

        while (node != null) {
            Object item2 = node.item;
            if (item.equals(item2)) {
                if (prevNode == null) { // only 1 element in list
                    node = null;
                    return true;
                } else {
                    prevNode.next = node.next;
                }
                result = true;
            }
            prevNode = node;
            node = node.next;
        }
        return result;
    }
}