简体   繁体   English

LinkedList的Java实现输出null

[英]Java Implementation of LinkedList outputting null

I'm having a bit of trouble here, I'm implementing my own Linked List class that does what you expect, it adds elements to a list and using a ToString method, outputs them. 我在这里遇到了麻烦,我正在实现自己的Linked List类,该类可以完成您所期望的工作,它将元素添加到列表中并使用ToString方法输出它们。 For some reason, it adds elements to the list fine but when it's time to output the list, it adds a null element to the front and ignores printing the last element. 由于某种原因,它可以将元素很好地添加到列表中,但是当需要输出列表时,它会在前面添加一个null元素,而忽略最后一个元素的打印。 Here is my main class code: 这是我的主要课程代码:

public class MyList {
    public static void main(String[] args) {
        List<Integer> list = new LinkedList<>();
        list.add(1);                 // [1]
        list.add(2);                 // [1 2]
        list.add(3);                 // [1 2 3]
        System.out.println(list);
    }
}

And here's the add and ToString method: 这是add和ToString方法:

public class LinkedList<E>  {
private Node<E> first, last;
private int size = 0;

// new empty list constructor.
public LinkedList() {
    first = last = new Node<>(null, null);
}

public void add(E e) {
    last.next = new Node<>(e, null);
    last = last.next;
    ++size;
}

public void addFirst(E e) {
    Node<E> n = new Node<>(e, null);
    n.next = first.next;
    first = n;
    ++size;

}

public String toString() {
    try {
        if (first != null) {
            Node<E> n = first;
            String s = "[ ";

            while (n.next != null) {
                s = s + n.data + " ";
                n = n.next;
            }

            return s + "]";
        }
    } catch (NoSuchElementException e) {
        //return "List is empty!";
    }

    return "List is empty!";
}

And when trying to compile the above, I get this output (when it should be [ 1 2 3 ] ): 当尝试编译上面的代码时,我得到以下输出(应为[ 1 2 3 ] ):

> [ null 1 2 ]

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thank you. 谢谢。

This line: 这行:

last.next = new Node<>(e, null);

should throw a NullPointerException when you add the first element to the list, because first and last should be null when the list is empty. 将第一个元素添加到列表时, 应该抛出NullPointerException ,因为当列表为空时firstlast应该为null The fact that it's apparently not erroring out on you means that last must be pointing to a valid Node object before you've added anything to your list. 它显然不会对您造成错误的事实意味着,在将任何内容添加到列表之前, last必须指向有效的Node对象。 You don't have all of your code in your post, but I'm pretty confident that you're creating a new Node and pointing first and last to it when you call your list's constructor, which means that your list will start with an "empty" node. 您的帖子中没有所有代码,但是我非常有信心,您要创建一个新的Node并在调用列表的构造函数时first指向它, last指向它,这意味着您的列表将以“空”节点。

What you should be doing is starting with both first and last equal to null . 应该做的是从firstlast等于null Then do something like this in your add() method: 然后在add()方法中执行以下操作:

public void add(E e) {
    Node newNode = new Node<>(e, null);
    if(first == null) {
        first = newNode;
        last = newNode;
    } else {
        last.next = newNode;
        last = last.next;  // Or just last = newNode;
    }
    ++size;
}

Also, you need to work on your toString() method a bit, because this line: 另外,您需要对toString()方法进行一些操作,因为此行:

while (n.next != null) {

will cause your loop to bypass the last element in the list (since its correct for the list element's next to be null). 会导致您的循环绕过列表中的最后一个元素(因为它对列表元素的next为null正确)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM