在SinglyLinkedList中的head之后的下一个节点的NullPointerException

时间:2015-03-25 15:36:37

标签: java

所以我试图以递归方式检索列表中的项目并打印它们并编写此函数来执行此操作

public Key getRecursive(int goal, int current, Node<Key> item) {
    if (goal == current) {
        return item.getValue();
    }//base
    else {
        getRecursive(goal, current++, item.getNext()); //exception here
    }
    return null;
}

我在这里调用它,在给我一个NullPointerException之前打印15。

SinglyLinkedList myList = new SinglyLinkedList();

    myList.add(15);
    myList.add(23);
    myList.add(24);
    myList.add(-6);

    for (int i = 0; i < myList.size(); i++) {
        System.out.println(myList.getRecursive(i, 0, myList.getHead()));
    } //for

该列表有四个节点,为什么15之后的下一个节点被视为空?

完全单链接列表

class SinglyLinkedList<Key> {

private static class Node<Key> {

    private Key value;
    private Node<Key> next;

    public Node(Key newValue, Node<Key> nextNode) {
        value = newValue;
        next = nextNode;
    } //Node (constructor)

    public Key getValue() {
        return value;
    } //getValue

    public void setValue(Key newValue) {
        value = newValue;
    } //setValue

    public Node<Key> getNext() {
        return next;
    } //getNext

    public void setNext(Node<Key> newNext) {
        next = newNext;
    } //setNext

    public boolean hasNext() {
        if (next == null) {
            return false;
        } else {
            return true;
        } //if-else
    } //hasNext

} //Node (class)

// Add function goes here
private Node<Key> head;
private Node<Key> tail;
private int size;

public SinglyLinkedList() {
    head = null;
    tail = null;
    size = 0;
} //SinglyLinkedList (constructor)

public int size() {
    return size;
} //size

public boolean isEmpty() {
    if (size == 0) {
        return true;
    } else {
        return false;
    } //if-else
} //isEmpty

public void add(Key newInt) {
    Node<Key> newest = new Node(newInt, null);

    if (isEmpty()) {
        head = newest;
    } else {
        tail.setNext(newest);
    } //if

    tail = newest;
    size++;
} //add

public Node<Key> getHead(){
    return head;
}

public void addFirst(Key newInt) {
    Node<Key> newest = new Node(newInt, head);

    if (isEmpty()) {
        tail = newest;
    } //if

    head = newest;
    size++;
} //addFirst

public Key get(int index) {
    if (index > size) {
        return null;
    } //if

    Node<Key> current = head;
    for (int i = 0; i < index; i++) {
        current = current.getNext();
    } //for

    return current.getValue();

} //get

public void remove(int location) {
    Node<Key> currentLocation, previousLocation;

    if (location == 0) {
        head = head.getNext();
    } else {
        previousLocation = head;
        currentLocation = head.getNext();

        for (int i = 0; i < location-1; i++) {
            previousLocation = previousLocation.getNext();
            currentLocation = currentLocation.getNext();
        } //for

        previousLocation.setNext(currentLocation.getNext());

        if (location == size-1) {
            tail = previousLocation;
        } //if

    } //if-else

    size--;
} //remove

public Key getRecursive(int goal, int current, Node<Key> item) {
    if (goal == current) {
        return item.getValue();
    }//base
    else {
        getRecursive(goal, current++, item.getNext());
    }
    return null;
}

} // SinglyLinkedList(class)

1 个答案:

答案 0 :(得分:1)

public Key getRecursive(int goal, int current, Node<Key> item) {
    if (goal == current) {
        return item.getValue();
    }//base
    else {
        getRecursive(goal, current++, item.getNext()); //exception here
    }
    return null;
}

应该是

public Key getRecursive(int goal, int current, Node<Key> item) {
// TODO Check for legal input here!
    if (goal == current) {
        return item.getValue();
    }//base
    else {
        //return ( item.hasNext()?getRecursive(goal, current++, item.getNext()):null); <- will not work with current++ !!
        return ( item.hasNext()?getRecursive(goal, current+1, item.getNext()):null);
    }

}