简体   繁体   中英

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. I have overridden the toString method in the Node class to print the Node (value and next). 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?
  2. what needs to be changed to print only the Node I want in the 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.

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

That means the next Node will also have it's toString() method called. Then that node will call ITS next node's toString , and so on. 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.

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(). It's the same as

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

And by default, Object class has toString() method which returns:

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! 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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