Java 中的多个数组 toString

发布于 2024-10-16 05:04:47 字数 161 浏览 6 评论 0原文

我试图重写 Java 中的 toString 方法,将两个(字符串)数组输出到一个字符串中,然后我可以使用换行符对其进行格式化,我假设使用 /n。我刚刚开始掌握 Java,在查看了文档之后,我仍然对这样的语法应该是什么样子感到困惑。如果有人有一个可以向我展示的例子,或者可以解释这样做的好方法,我将不胜感激。

I'm trying to override the toString method in Java to output from two arrays (of strings) into a string which I can then format with line breaks, which I assume use /n. I'm just getting to grips with Java and after looking at the documentation I'm still baffled as to how the syntax for something like this should look. If someone has an example they could show me or can explain a good method of doing this I would be most grateful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨离汐 2024-10-23 05:04:47

如果您有一个包含两个数组的类,并且想要重写 toString 方法来显示您所做的:

@Override
public String toString() {
    return Arrays.toString(array1) + " " + Arrays.toString(array2);
}

这是一个完整的示例:

import java.util.Arrays;

public class Test {

    int[] array1 = { 1, 2, 3 };
    String[] array2 = { "Hello", "World" };

    @Override
    public String toString() {
        return Arrays.toString(array1) + " " + Arrays.toString(array2);
    }

    public static void main(String[] args) {
        System.out.println(new Test());
    }
}

输出:

[1, 2, 3] [Hello, World]

这是一个带有 new 的版本-lines:

public class Test {

    int[] array1 = { 1, 2, 3 };
    String[] array2 = { "Hello", "World" };

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("array1:\n");
        for (int i : array1)
            sb.append("    ").append(i).append('\n');

        sb.append("\narray2:\n");
        for (String s : array2)
            sb.append("    ").append(s).append('\n');

        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(new Test());
    }
}

输出:

array1:
    1
    2
    3

array2:
    Hello
    World

If you have a class with two arrays, and want to override the toString method to show these you do:

@Override
public String toString() {
    return Arrays.toString(array1) + " " + Arrays.toString(array2);
}

Here is a complete example:

import java.util.Arrays;

public class Test {

    int[] array1 = { 1, 2, 3 };
    String[] array2 = { "Hello", "World" };

    @Override
    public String toString() {
        return Arrays.toString(array1) + " " + Arrays.toString(array2);
    }

    public static void main(String[] args) {
        System.out.println(new Test());
    }
}

Output:

[1, 2, 3] [Hello, World]

Here is a version with new-lines:

public class Test {

    int[] array1 = { 1, 2, 3 };
    String[] array2 = { "Hello", "World" };

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("array1:\n");
        for (int i : array1)
            sb.append("    ").append(i).append('\n');

        sb.append("\narray2:\n");
        for (String s : array2)
            sb.append("    ").append(s).append('\n');

        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(new Test());
    }
}

Output:

array1:
    1
    2
    3

array2:
    Hello
    World

要覆盖 toString ,您需要创建一个新的类,因为它是从 Object 继承的,并且如果您创建一个内部有 2 个数组字符串的类,则它不需要两个参数那么你绝对可以做到这一点。

To overwrite toString you need to create a new Class since it's inherited from Object, and it doesn't take two arguments, if you make an Class that has 2 Array Strings inside it then you could definitely do this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文