我正在為我的資料結構類開發一個專案,該專案要求我撰寫一個類來實作一個整數鏈表。
- 為節點使用內部類。
- 包括以下方法。
- 撰寫一個測驗器,使您能夠以任何順序使用您想要的任何資料測驗所有方法。
我必須創建一個名為“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, ]。在最后一個元素之后,我得到一個額外的逗號和一個額外的空格。我該如何解決?
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;
}
}
}
}
uj5u.com熱心網友回復:
使用一個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 "]";
}
我們可以討論使用 a 的重要性StringBuilder以及所有這些,但你明白了
uj5u.com熱心網友回復:
跟蹤回圈外的節點并通過檢查下一個節點是否為空來迭代到倒數第二個節點。然后在 for 回圈之后追加最后一個節點的值。
IE
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;
uj5u.com熱心網友回復:
嘗試這個。
public String toString() {
return Stream.iterate(head, Objects::nonNull, Node::nextNode)
.map(n -> String.valueOf(n.value))
.collect(Collectors.joining(", ", "[", "]"));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/324389.html
上一篇:無法在JSP中決議匯入
下一篇:在Eclipse中匯入庫/JAR
