简体   繁体   English

覆盖toString Java以打印LinkedList节点

[英]Overriding toString Java to print LinkedList nodes

I have a Node Class and TestMain Class that I'm using to create and test a Linked List. 我有一个Node类和TestMain类,用于创建和测试链接列表。 I have overridden the toString method in the Node class to print the Node (value and next). 我已覆盖Node类中的toString方法以打印Node(值和下一个)。 But it's printing the List recursively. 但是它以递归方式打印列表。 I want to print only the Node I specify. 我只想打印我指定的节点。 Can someone tell me 有人可以告诉我

  1. why my toString is recursively printing the whole list? 为什么我的toString递归打印整个列表?
  2. what needs to be changed to print only the Node I want in the main() 什么需要更改以仅在main()中打印我想要的节点

 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 

You are trying to print getNext() as well in your toString() method. 您也尝试在toString()方法中打印getNext()

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

That means the next Node will also have it's toString() method called. 这意味着下一个Node也将调用它的toString()方法。 Then that node will call ITS next node's toString , and so on. 然后,该节点将调用ITS下一个节点的toString ,依此类推。 You need to remove that portion to avoid printing out the whole list. 您需要删除该部分以避免打印出整个列表。

    return "value = " +  this.value;

Then if you need to have the next node printed, you have to do it from outside the method. 然后,如果需要打印下一个节点,则必须从方法外部进行打印。 It shouldn't be toString()'s responsibility to print out next node values anyways. 无论如何,toString()都不应该打印出下一个节点值。

When you write 当你写

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

It implicitly calls for SomeObject class toString() method, which is inhereted from Object class toString(). 它隐式调用SomeObject类toString()方法,该方法从Object类toString()中引入。 It's the same as 和...一样

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

And by default, Object class has toString() method which returns: 默认情况下,Object类具有toString()方法,该方法返回:

public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

But you have overriden toString() method, so now it can't return "address" because you've changed the method! 但是,您已经覆盖了toString()方法,因此现在您不能返回“ address”,因为您已经更改了该方法! You can try this code: 您可以尝试以下代码:

public class Node {
private int value;
private Node next;
private String address=getClass().getName() + "@" + Integer.toHexString(hashCode());

public String getAddress() {
    return this.address;
}

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 = " + getNextAddress();
}

private String getNextAddress() {
    if(getNext()==null){
        return "null";
    }
    return getNext().getAddress();
}


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

}}

It's not the art of programming, but I hope it works as you wish. 这不是编程的艺术,但我希望它能如您所愿。

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

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