二维数组转字符串代码java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20035669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:32:04  来源:igfitidea点击:

2 dimensional array to string code java

javaarraystostringmultidimensional-array

提问by Alex A

For a program I have to write I am required to turn a 2 dimensional array of single digit intgers into a string of numbers separated by spaces. A test code is provided for me and when I run it, my code fails both arrayToString tests. I am unable to find where my logic error is. Someone help!

对于我必须编写的程序,我需要将单个数字整数的二维数组转换为由空格分隔的数字字符串。为我提供了一个测试代码,当我运行它时,我的代码在两个 arrayToString 测试中都失败了。我找不到我的逻辑错误在哪里。有人帮忙!

public static String arrayToString(int[][] a) {

    String aString;     
    aString = "";
    int column;
    int row;

    for (row = 0; row < a.length; row++) {
        for (column = 0; column < a[0].length; column++ ) {
        aString = aString + " " + a[row][column];
        }
    aString = aString + "\n";
    }

    return aString;
}

回答by Bohemian

Your code is basically OK, except you are putting a leading space at the start of every line. To clean them up before returning, add this line:

您的代码基本上没问题,只是您在每一行的开头放置了一个前导空格。要在返回之前清理它们,请添加以下行:

aString = aString.replaceAll("(?m)^ ", "");

This uses regex to delete (by replacing with a blank) all spaces at the start of lines. The regex flag (?m)makes ^match the start of every line of the input (normally ^matches only the start of input).

这使用正则表达式删除(通过用空格替换)行首的所有空格。正则表达式标志(?m)品牌^匹配输入的每一行(通常的开始^只输入开始相匹配)。

回答by Dawood ibn Kareem

I can think of three possible issues.

我能想到三个可能的问题。

  1. The test code doesn't want to see the space that you are putting at the beginning of every line.
  2. The test code is passing an array where some of the entries are null. Your code doesn't cope with this.
  3. The test code is passing an array where the entries have different lengths. Your code doesn't cope with this either.
  1. 测试代码不想看到您在每一行开头放置的空间。
  2. 测试代码正在传递一个数组,其中一些条目为空。您的代码无法解决此问题。
  3. 测试代码正在传递一个数组,其中条目具有不同的长度。您的代码也无法解决此问题。

Until you post the test code, we can't see which one the problem is. But you could deal with all three by changing your loop to this.

在您发布测试代码之前,我们无法看到问题出在哪一个。但是您可以通过将循环更改为此来处理所有三个。

for (row = 0; row < a.length; row++) {
    if (a[row] != null && a[row].length > 0) {
        aString = aString + a[row][0];

        for (column = 1; column < a[row].length; column++) {
            aString = aString + " " + a[row][column];
        }
    }
    aString = aString + "\n";
}

回答by Martin

int[][] a = new int[][]{{1,2,3},{4,5,6},{7,8,9}};

public static String arrayToString(int[][] a) {

    String aString;     
    aString = "";
    int column;
    int row;

    for (row = 0; row < a.length; row++) {
        for (column = 0; column < a[0].length; column++ ) {
        aString = aString + " " + a[row][column];
        }
    aString = aString + "\n";
    }

    return aString;
}

Output:

输出:

1 2 3
4 5 6
7 8 9

If you skip this line aString = aString + "\n";your output will look like this

如果您跳过这一行,aString = aString + "\n";您的输出将如下所示

1 2 3 4 5 6 7 8 9

回答by Valentin Michalak

@dawood-ibn-kareem explain good the possible cause of the failure of the test.

@dawood-ibn-kareem 很好地解释了测试失败的可能原因。

With Java 8 Stream, it's pretty easy to do what you want!

使用 Java 8 Stream,可以很容易地做你想做的事!

With the input: [[1,2,3], [3,4,5], [5,6,7]]

随着输入:[[1,2,3], [3,4,5], [5,6,7]]

public static String arrayToString(int[][] a) {
    return Arrays.stream(a)
            .map(s -> Arrays.stream(s)
                            .mapToObj(String::valueOf)
                            .collect(Collectors.joining(" "))
            )
            .collect(Collectors.joining("\n"));
}

Output:

输出:

1 2 3
3 4 5
5 6 7

Other method, to separate each line of the 2D array without carriage return

其他方法,将二维数组的每一行分开,不用回车

public static String arrayToString(int[][] a) {
    return Arrays.stream(a)
            .flatMapToInt(Arrays::stream)
            .mapToObj(String::valueOf)
            .collect(Collectors.joining(" "));
}

Output

输出

1 2 3 3 4 5 5 6 7

Have fun!

玩得开心!