java - SinglyLinkedList 中头之后的下一个节点出现 NullPointerException

标签 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(类)

最佳答案

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);
    }

}

关于java - SinglyLinkedList 中头之后的下一个节点出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29260071/

相关文章:

javascript - 可以是 String 或 Boolean 的 Spring 和 Json 字段映射

java - 我应该拆分 Spring 上下文文件吗

Java:为什么我们需要转换 float 而不是 double ?

java - 使用 Map 接口(interface)将元素放入 HashMap

java - 获取特定 xml 元素的 xsd 限制

java - Catmull Rom 样条实现 (LibGDX)

java - 如何从命令提示符将 jar 反编译为 .java 文件

java - DocuSign 自动回复收件人事件

java - shiro 在身份验证 web 应用程序上按角色重定向

java - 使用 GWT 和 Protege 在 Eclipse 上创建搜索应用程序