如何修复我的 toString 方法中的输出?
How can I fix my output in my toString method?
我正在为我的数据结构 class 开发一个项目,该项目要求我编写一个 class 来实现一个整数链表。
- Use an inner class for the Node.
- Include the methods below.
- Write a tester to enable you to test all of the methods with whatever data you want in any order.
我必须创建一个名为“public String toString()”的 toString 方法。此方法旨在“打印列表,但格式为 [1, 2, 3],逗号和括号位于正确的位置。”我在下面有这个方法的代码。但是,当我使用 toString 方法时,出现了一些问题。例如,如果我有一个列表 [17、14、8、11、19、6、15、11、5、5]。我的输出是 [17, 14, 8, 11, 19, 6, 15, 11, 5, 5, ]。在最后一个元素之后,我在末尾得到了一个额外的逗号和一个额外的 space 。我该如何解决这个问题?
import java.util.Random;
import java.util.Scanner;
public class LinkedListOfInts {
Node head;
Node tail;
private class Node {
int value;
Node nextNode;
public Node(int value, Node nextNode) {
this.value = value;
this.nextNode = nextNode;
}
}
public LinkedListOfInts(LinkedListOfInts other) {
Node tail = null;
for (Node n = other.head; n != null; n = n.nextNode) {
if (tail == null)
this.head = tail = new Node(n.value, null);
else {
tail.nextNode = new Node(n.value, null);
tail = tail.nextNode;
}
}
}
public LinkedListOfInts(int[] other) {
Node[] nodes = new Node[other.length];
for (int index = 0; index < other.length; index++) {
nodes[index] = new Node(other[index], null);
if (index > 0) {
nodes[index - 1].nextNode = nodes[index];
}
}
head = nodes[0];
}
public LinkedListOfInts(int N, int low, int high) {
Random random = new Random();
for (int i = 0; i < N; i++)
this.addToFront(random.nextInt(high - low) + low);
}
public void addToFront(int x) {
head = new Node(x, head);
}
public String toString() {
String result = "";
for (Node ptr = head; ptr != null; ptr = ptr.nextNode)
result += ptr.value + ", ";
return "[" + result + "]";
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
LinkedListOfInts list = new LinkedListOfInts(10, 1, 20);
boolean done = false;
while (!done) {
System.out.println("1. toString");
switch (input.nextInt()) {
case 1:
System.out.println("toString");
System.out.println(list.toString());
break;
}
}
}
}
跟踪循环外的节点并通过检查 next 节点是否为空来迭代直到倒数第二个节点。然后在 for 循环之后附加最后一个节点的值。
即
public String toString() {
String result = "";
Node ptr;
for (ptr = head; ptr.nextNode != null; ptr = ptr.nextNode) {
result += ptr.value + ", ";
result += ptr.value + "]";
result = "[" + result;
return result;
使用 StringJoiner
,例如...
public String toString() {
StringJoiner sj = new StringJoiner(", ", "[", "]");;
for (Node ptr = head; ptr != null; ptr = ptr.nextNode) {
sj.add(Integer.toString(ptr.value));
}
return sj.toString();
}
然后使用类似...
LinkedListOfInts list = new LinkedListOfInts(new int[]{17, 14, 8, 11, 19, 6, 15, 11, 5, 5});
System.out.println(list.toString());
将打印...
[17, 14, 8, 11, 19, 6, 15, 11, 5, 5]
现在,如果您不能使用 StringJoiner
,请不要附加 ,
而是添加前缀,例如...
public String toString() {
String result = "";
for (Node ptr = head; ptr != null; ptr = ptr.nextNode) {
if (!result.isEmpty()) {
result += ", ";
}
result += ptr.value;
}
return "[" + result + "]";
}
我们可以讨论使用 StringBuilder
的重要性等等,但你明白了
试试这个。
public String toString() {
return Stream.iterate(head, Objects::nonNull, Node::nextNode)
.map(n -> String.valueOf(n.value))
.collect(Collectors.joining(", ", "[", "]"));
}
我正在为我的数据结构 class 开发一个项目,该项目要求我编写一个 class 来实现一个整数链表。
- Use an inner class for the Node.
- Include the methods below.
- Write a tester to enable you to test all of the methods with whatever data you want in any order.
我必须创建一个名为“public String toString()”的 toString 方法。此方法旨在“打印列表,但格式为 [1, 2, 3],逗号和括号位于正确的位置。”我在下面有这个方法的代码。但是,当我使用 toString 方法时,出现了一些问题。例如,如果我有一个列表 [17、14、8、11、19、6、15、11、5、5]。我的输出是 [17, 14, 8, 11, 19, 6, 15, 11, 5, 5, ]。在最后一个元素之后,我在末尾得到了一个额外的逗号和一个额外的 space 。我该如何解决这个问题?
import java.util.Random;
import java.util.Scanner;
public class LinkedListOfInts {
Node head;
Node tail;
private class Node {
int value;
Node nextNode;
public Node(int value, Node nextNode) {
this.value = value;
this.nextNode = nextNode;
}
}
public LinkedListOfInts(LinkedListOfInts other) {
Node tail = null;
for (Node n = other.head; n != null; n = n.nextNode) {
if (tail == null)
this.head = tail = new Node(n.value, null);
else {
tail.nextNode = new Node(n.value, null);
tail = tail.nextNode;
}
}
}
public LinkedListOfInts(int[] other) {
Node[] nodes = new Node[other.length];
for (int index = 0; index < other.length; index++) {
nodes[index] = new Node(other[index], null);
if (index > 0) {
nodes[index - 1].nextNode = nodes[index];
}
}
head = nodes[0];
}
public LinkedListOfInts(int N, int low, int high) {
Random random = new Random();
for (int i = 0; i < N; i++)
this.addToFront(random.nextInt(high - low) + low);
}
public void addToFront(int x) {
head = new Node(x, head);
}
public String toString() {
String result = "";
for (Node ptr = head; ptr != null; ptr = ptr.nextNode)
result += ptr.value + ", ";
return "[" + result + "]";
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
LinkedListOfInts list = new LinkedListOfInts(10, 1, 20);
boolean done = false;
while (!done) {
System.out.println("1. toString");
switch (input.nextInt()) {
case 1:
System.out.println("toString");
System.out.println(list.toString());
break;
}
}
}
}
跟踪循环外的节点并通过检查 next 节点是否为空来迭代直到倒数第二个节点。然后在 for 循环之后附加最后一个节点的值。
即
public String toString() {
String result = "";
Node ptr;
for (ptr = head; ptr.nextNode != null; ptr = ptr.nextNode) {
result += ptr.value + ", ";
result += ptr.value + "]";
result = "[" + result;
return result;
使用 StringJoiner
,例如...
public String toString() {
StringJoiner sj = new StringJoiner(", ", "[", "]");;
for (Node ptr = head; ptr != null; ptr = ptr.nextNode) {
sj.add(Integer.toString(ptr.value));
}
return sj.toString();
}
然后使用类似...
LinkedListOfInts list = new LinkedListOfInts(new int[]{17, 14, 8, 11, 19, 6, 15, 11, 5, 5});
System.out.println(list.toString());
将打印...
[17, 14, 8, 11, 19, 6, 15, 11, 5, 5]
现在,如果您不能使用 StringJoiner
,请不要附加 ,
而是添加前缀,例如...
public String toString() {
String result = "";
for (Node ptr = head; ptr != null; ptr = ptr.nextNode) {
if (!result.isEmpty()) {
result += ", ";
}
result += ptr.value;
}
return "[" + result + "]";
}
我们可以讨论使用 StringBuilder
的重要性等等,但你明白了
试试这个。
public String toString() {
return Stream.iterate(head, Objects::nonNull, Node::nextNode)
.map(n -> String.valueOf(n.value))
.collect(Collectors.joining(", ", "[", "]"));
}