0

我有一个节点类和 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

4

2 回答 2

0

您也尝试getNext()在 toString() 方法中进行打印。

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

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

    return "value = " +  this.value;

然后,如果您需要打印下一个节点,则必须从方法外部进行。无论如何,打印下一个节点值不应该是 toString() 的责任。

于 2013-09-17T20:13:29.647 回答
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() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

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

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

}}

这不是编程的艺术,但我希望它能如你所愿。

于 2013-09-18T11:28:16.377 回答