重写toString Java以打印LinkedList节点
我有一个Node类和TestMain类,用于创建和测试链接列表。我已经重写了Node类中的toString方法来打印节点(value和next)。但它正在递归地打印列表。我只想打印我指定的节点。有人能告诉我吗重写toString Java以打印LinkedList节点,java,recursion,reference,tostring,singly-linked-list,Java,Recursion,Reference,Tostring,Singly Linked List,我有一个Node类和TestMain类,用于创建和测试链接列表。我已经重写了Node类中的toString方法来打印节点(value和next)。但它正在递归地打印列表。我只想打印我指定的节点。有人能告诉我吗 为什么我的toString递归地打印整个列表 在main()中只打印我想要的节点需要更改什么 public class Node { private int value; private Node next; Node(int value){ t
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
公共类节点{
私有int值;
私有节点下一步;
节点(int值){
这个。值=值;
}
public int getValue(){
返回值;
}
公共无效设置值(int值){
这个值=值;
}
公共节点getNext(){
下一步返回;
}
公共void setNext(节点next){
this.next=next;
}
公共字符串toString(){
返回“value=“+this.value+”,next=“+getNext();
}
}
公共类TestMain{
公共静态void main(字符串[]args){
System.out.println(“begintestmain\n”);
节点头=新节点(10);
节点n1=新节点(11);
节点n2=新节点(12);
节点n3=新节点(13);
头。下一个(n1);
n1.setNext(n2);
n2.设置下一步(n3);
System.out.println(“Head:+Head”);
系统输出println(“n1:+n1”);
系统输出打印项次(“n2:+n2”);
系统输出打印项次(“n3:+n3”);
System.out.println(“\nEnd TestMain”);
}
}
//>>>>>>输出您正试图在toString()方法中打印getNext()
这意味着下一个节点也将调用它的toString()
方法。然后该节点将调用其下一个节点的toString
,依此类推。您需要删除该部分以避免打印出整个列表
return "value = " + this.value;
然后,如果需要打印下一个节点,则必须从方法外部执行。无论如何,toString()不应该负责打印下一个节点值。当您编写
SomeObject对象=新建SomeObject()代码>
System.out.println(对象)代码>
它隐式地调用SomeObject类toString()方法,该方法是从Object类toString()继承的。这和
SomeObject对象=新建SomeObject()代码>
System.out.println(object.toString())代码>
默认情况下,对象类具有toString()方法,该方法返回:
公共字符串toString(){
返回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");
}}
这不是编程的艺术,但我希望它能如您所愿工作。它正在打印后续节点,因为您告诉它打印后续节点。代码是你写的@DavidWallace-如何修改toString以打印下一个节点的“值”和“地址”?如果要打印下一个节点的地址怎么办?(如果是结束,则为null)然后从方法外部执行。在节点上调用toString(),然后在节点上调用toString()。getNext()节点。我不确定我是否正确理解您的意思。如何更改节点的toString方法以打印“值”和“下一个节点的地址”。不能通过修改toString方法来完成吗?@MyFirstNameMyLastName是的,可以完成。您可以编写类似于return“value=“+this.value+”,next=“+getNext().getValue代码>我想打印2件东西,1件。变量值中的值,2是下一个变量的值。应该是10左右,测试。Node@4fe5e2c3.Thanks!! 这正是我们想让它发挥作用的方式。谢谢。
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");
}}