簡體   English   中英

覆蓋toString Java以打印LinkedList節點

[英]Overriding toString Java to print LinkedList nodes

我有一個Node類和TestMain類,用於創建和測試鏈接列表。 我已覆蓋Node類中的toString方法以打印Node(值和下一個)。 但是它以遞歸方式打印列表。 我只想打印我指定的節點。 有人可以告訴我

  1. 為什么我的toString遞歸打印整個列表?
  2. 什么需要更改以僅在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 

您也嘗試在toString()方法中打印getNext()

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

這意味着下一個Node也將調用它的toString()方法。 然后,該節點將調用ITS下一個節點的toString ,依此類推。 您需要刪除該部分以避免打印出整個列表。

    return "value = " +  this.value;

然后,如果需要打印下一個節點,則必須從方法外部進行打印。 無論如何,toString()都不應該打印出下一個節點值。

當你寫

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() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

但是,您已經覆蓋了toString()方法,因此現在您不能返回“ address”,因為您已經更改了該方法! 您可以嘗試以下代碼:

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

}}

這不是編程的藝術,但我希望它能如您所願。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM