繁体   English   中英

如何修复我的 toString 方法中的输出?

[英]How can I fix my output in my toString method?

我正在为我的数据结构类开发一个项目,该项目要求我编写一个类来实现一个整数链表。

  • 为节点使用内部类。
  • 包括以下方法。
  • 编写一个测试器,使您能够以任何顺序使用您想要的任何数据测试所有方法。

我必须创建一个名为“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;
            }
        }
    }
}

使用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 🙄,请不要附加,而是在它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的重要性以及所有这些,但您明白了

跟踪循环外的节点并通过检查下一个节点是否为空来迭代到倒数第二个节点。 然后在 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;

尝试这个。

public String toString() {
    return Stream.iterate(head, Objects::nonNull, Node::nextNode)
        .map(n -> String.valueOf(n.value))
        .collect(Collectors.joining(", ", "[", "]"));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM