linkedlist实现中的tostring()方法存在问题JAVA

mpbci0fu  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(324)

此问题已在此处找到答案

什么是nullpointerexception,如何修复它((12个答案)
三天前关门。
我只是尝试实现一个链表,当我只添加了两个元素时,它抛出了 NullPointerExceptiontoString 方法,但我不知道为什么。当它为空时,它可以打印[],但随后它会抛出一个错误。也许你能帮我找出我的手机出了什么问题 toString() 方法?
如果列表为空,则理想输出为[a、b、c]或[]。
我的全部代码:

import java.util.Iterator;
import java.util.NoSuchElementException;

public class ListImpl implements List {
    private Node head;
    private Node tail;

    private static class Node {
        Object data;
        Node next;

        Node(Object d) {
            data = d;
            next = null;
        }
    }

    @Override
    public void clear() {
        if (size() > 0) {
            head = null;
            tail = null;
        }
    }

    @Override
    public int size() {
        int size = 0;
        Node currentNode = head;
        while (currentNode != null) {
            size++;
            currentNode = currentNode.next;
        }
        return size;
    }

    public Iterator<Object> iterator() {
        return new IteratorImpl();
    }

    private class IteratorImpl implements Iterator<Object> {

        @Override
        public boolean hasNext() {
            return head != null;
        }

        @Override
        public Object next() {
            if(hasNext()){
                Object data = head.data;
                head = head.next;
                return data;
            }
            return null;
        }
    }

    @Override
    public void addFirst(Object element) {
        Node newNode = new Node(element);
        newNode.next = null;
        if (head == null) {
            head = newNode;
        }
        else {
            newNode.next = head;
            head = newNode;
        }
    }

    @Override
    public void addLast(Object element) {
        Node newNode = new Node(element);
        newNode.next = null;
        if (tail == null) {
            tail = newNode;
        }
        else {
            newNode.next = tail;
            tail = newNode;
        }

    }

    @Override
    public void removeFirst() {
        if (head == null) {
            System.err.print("The first element is absent");
        }
        head = head.next;
    }

    @Override
    public void removeLast() {
        if (head == null)
            System.err.print("Your list is empty");

        if (head.next == null) {
            System.err.print("There is only one element");
        }

        // Find the second last node
        Node second_last = head;
        while (second_last.next.next != null)
            second_last = second_last.next;

        // Change next of second last
        second_last.next = null;
    }

    @Override
    public Object getFirst() {
        if(head!=null) {
            return head.data;}
        else
            throw new java.util.NoSuchElementException("List is empty");
    }

    @Override
    public Object getLast() {
        if(head == null){
            throw new java.util.NoSuchElementException("List is empty");
        }
            Node last = head;
        while (last.next != null)
        {
            last = last.next;
        }
        return last;
    }

    @Override
    public Object search(Object element) {
        Object result = null;
        while (head.next != null)
        {
            if (head.data.equals(element)){
               result = head.data;
            }
        }
        return result;
    }

    @Override
    public boolean remove(Object element) {
        boolean isFound = false;
        if(head == null) {
            throw new NoSuchElementException("List is empty!");
        }
        if(head.data.equals(element)) {
            head = head.next;
            return true;
        }

        Node currentNode = head;
        Node previousNode = null;
        while(currentNode !=null) {
            if(currentNode.data.equals(element)) {
                isFound = true;
                break;
            }
            previousNode = currentNode;
            currentNode = currentNode.next;
        }
        if(currentNode == null) {
            return isFound;
        }
        currentNode = previousNode.next;
        previousNode.next = currentNode.next;
        currentNode.next = null;
        return isFound;
    }

    @Override
    public String toString() {
        Node current = head;
        if(head == null) {
            return "[]";
        }
       StringBuilder result = new StringBuilder();
        result.append("[");
        while(current.next != null) {
            result.append(current.data + ", ");
            current = current.next;
        }
        result.append(tail.data + "]");
        return result.toString();
    }

    public static void main(String[] args) {
    ListImpl list = new ListImpl();
    list.addFirst("FirstElement");
    list.addFirst("SecondElement");
        System.out.println(list);
    }
}
yvfmudvl

yvfmudvl1#

问题就在眼前

result.append(tail.data + "]");

在方法中不更新尾部 addFirst() 在其他方法中,如果你纠正了这一点,它也会起作用。
请随时对我的回答添加评论,以进行澄清。

相关问题