繁体   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