在Java中打印双向链接列表将引发异常(堆栈溢出异常)

时间:2019-03-17 05:39:31

标签: java

我有一个双向链表实现,如下所示。但是,当我尝试打印列表时,出现堆栈溢出异常。我不确定,但它看起来与重写toString()方法有关。有人可以解释一下到底是什么造成了这个问题。

        // Main function class
        public class Main {
            public static void main(String[] args) {
                DoublyLinkedList list = new DoublyLinkedList();
                list.insertAtStart(2);
                list.insertAtStart(3);
                list.insertAtStart(4);
                list.insertAtStart(5);
                list.insertAtStart(6);
                list.insertAtStart(7);
                list.print();
            }
        }
        // Doubly node class
        public class DoublyLinkedListNode {
            private int data;
            private DoublyLinkedListNode next;
            private DoublyLinkedListNode prev;
            public DoublyLinkedListNode(int data)
            {
                this.data = data;
            }

            public int getData() {
                return data;
            }

            public void setData(int data) {
                this.data = data;
            }

            public DoublyLinkedListNode getNext() {
                return next;
            }

            public void setNext(DoublyLinkedListNode next) {
                this.next = next;
            }

            public DoublyLinkedListNode getPrev() {
                return prev;
            }

            public void setPrev(DoublyLinkedListNode prev) {
                this.prev = prev;
            }

            @Override
            public String toString() {
                return "DoublyLinkedListNode{" +
                        "data=" + data +
                        ", next=" + next +
                        ", prev=" + prev +
                        '}';
            }
        }
        // Here I have created function for inserting and printing the elemnts in doubly linked list
        public class DoublyLinkedList {
            public DoublyLinkedListNode head;
            public void insertAtStart(int data)
            {
                DoublyLinkedListNode newNode = new DoublyLinkedListNode(data);
                if(head == null)
                {
                    head = newNode;
                }
                else
                {
                    newNode.setNext(head);
                    head.setPrev(newNode);
                    head = newNode;
                }
            }
           @Override
          public String toString() {
               return "DoublyLinkedList{" +
                       "head=" + head +
                       '}';
           }
            public void print()
            {
                DoublyLinkedListNode currentNode = head;
                while(currentNode != null)
                {
                    System.out.print(currentNode);
                    System.out.print("<=>");
                    currentNode = currentNode.getNext();
                }
                System.out.println("null");
            }
        }

1 个答案:

答案 0 :(得分:3)

此代码可导致无限递归。...

byte[]

逐步进行。

  • 将头节点转换为字符串时,它想将下一个节点转换为字符串。

  • 将第二个节点转换为字符串时,它想将前一个节点(再次是头节点)转换为字符串。

Infinite Recursion导致无限嵌套的方法调用,每次调用都会增加堆栈,直到耗尽堆栈并获得堆栈溢出为止。