覆盖toString Java以打印链接列表节点

时间:2013-09-17 20:11:58

标签: java recursion reference tostring singly-linked-list

我有一个Node Class和TestMain Class,我用它来创建和测试链接列表。我已经重写了Node类中的toString方法来打印Node(值和next)。但它以递归方式打印List。我想只打印我指定的节点。有人可以告诉我

  1. 为什么我的toString以递归方式打印整个列表?
  2. 需要更改什么才能在main()
  3. 中仅打印我想要的节点

    public class Node {
        private int value;
        private Node next;
    
        Node(int value){
            this.value=value; 
        }
    
        public int getValue() {
            return value;
        }
    
        public void setValue(int value) {
            this.value = value;
        }
    
        public Node getNext() {
            return next;
        }
    
        public void setNext(Node next) {
            this.next = next;
        }
    
        public String toString(){
            return "value = " +  this.value + ", next = " + getNext();
        }
    }
    
    
    public class TestMain {
    
        public static void main(String[] args) {
            System.out.println("Begin TestMain \n");
    
            Node head = new Node(10);
            Node n1 = new Node(11);
            Node n2 = new Node(12);
            Node n3 = new Node(13);
    
            head.setNext(n1);
            n1.setNext(n2);
            n2.setNext(n3);
    
            System.out.println("Head : " + head);
            System.out.println("n1 : " + n1);
            System.out.println("n2 : " + n2);
            System.out.println("n3 : " + n3);
    
            System.out.println("\nEnd TestMain");
    
        }
    
    }
    
    
    //>>>>>> output <<<<<<<<<
    Begin TestMain 
    
    Head : value = 10, next = value = 11, next = value = 12, next = value = 13, next = null
    n1 : value = 11, next = value = 12, next = value = 13, next = null
    n2 : value = 12, next = value = 13, next = null
    n3 : value = 13, next = null
    
    End TestMain
    
    //>>>>> Expected Output <<<<<<<<
    Begin TestMain 
    
    Head : value = 10, next = addressOf-n1
    n1 : value = 11, next = addressOf-n2
    n2 : value = 12, next = addressOf-n3
    n3 : value = 13, next = null
    
    End TestMain
    

    public class Node { private int value; private Node next; Node(int value){ this.value=value; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } public String toString(){ return "value = " + this.value + ", next = " + getNext(); } } public class TestMain { public static void main(String[] args) { System.out.println("Begin TestMain \n"); Node head = new Node(10); Node n1 = new Node(11); Node n2 = new Node(12); Node n3 = new Node(13); head.setNext(n1); n1.setNext(n2); n2.setNext(n3); System.out.println("Head : " + head); System.out.println("n1 : " + n1); System.out.println("n2 : " + n2); System.out.println("n3 : " + n3); System.out.println("\nEnd TestMain"); } } //>>>>>> output <<<<<<<<< Begin TestMain Head : value = 10, next = value = 11, next = value = 12, next = value = 13, next = null n1 : value = 11, next = value = 12, next = value = 13, next = null n2 : value = 12, next = value = 13, next = null n3 : value = 13, next = null End TestMain //>>>>> Expected Output <<<<<<<< Begin TestMain Head : value = 10, next = addressOf-n1 n1 : value = 11, next = addressOf-n2 n2 : value = 12, next = addressOf-n3 n3 : value = 13, next = null End TestMain

2 个答案:

答案 0 :(得分:0)

您正尝试在toString()方法中打印getNext()

return "value = " +  this.value + ", next = " + getNext();

这意味着下一个Node也会调用它的toString()方法。然后该节点将调用ITS下一个节点的toString,依此类推。您需要删除该部分以避免打印出整个列表。

    return "value = " +  this.value;

然后,如果您需要打印下一个节点,则必须从方法外部执行此操作。不应该是toString()负责打印出下一个节点值。

答案 1 :(得分:0)

写作时

SomeObject object = new SomeObject();
System.out.println(object);

它隐式调用SomeObject类toString()方法,该方法来自Object类toString()。它与

相同

SomeObject object = new SomeObject();
System.out.println(object.toString());

默认情况下,Object类具有toString()方法,该方法返回:

public String toString() { {
{1}} {
{1}}

但是你已经覆盖了toString()方法,所以现在它不能返回“地址”,因为你已经改变了方法!您可以尝试以下代码:


        return getClass().getName() + "@" + Integer.toHexString(hashCode());

这不是编程的艺术,但我希望它能按你的意愿运作。