如何将一个二维数字数组转换为一个二维字符串数组?

huangapple go评论71阅读模式
英文:

How can I convert a 2d Number array into a 2d String array?

问题

public class HelloWorld {
    public static void main(String[] args) {
        String[][] strings = new String[10][12];
        for (int i = 0; i < strings.length; i++) {
            for (int j = 0; j < strings[i].length; j++) {
                strings[i][j] = String.valueOf(Math.floor(Math.random() * 100000) / 100000);
                System.out.print(strings[i][j] + " ; ");
            }
            System.out.println();
        }
    }
}
英文:

I'm really new to java or programming, so i have a "basic" quesion (I guess). I have this code, which generates me an array of numbers. I want to convert that into an array of strings. Can someone please fix my code?

public class HelloWorld{
    public static void main(String[] args) {
        Number[][] values = new Number[10][12];
        for (int i = 0; i &lt; values.length; i++) {
            for (int j = 0; j &lt; values[i].length; j++) {
                values[i][j] = Math.floor(Math.random() * 100000) / 100000;

                System.out.print(values[i][j] + &quot; ; &quot;);
            }
            System.out.println();
        }
    }
}

答案1

得分: 2

你可以直接使用字符串数组而不是数字数组。在设置元素时,使用 String.valueOf(YOUR_NUMBER)

英文:

You can simply use String array instead of Number array. When setting the element, use String.valueOf(YOUR_NUMBER)

答案2

得分: 1

最明显的方法:

public static String[][] convert(Number[][] numbers) {
    String[][] result = new String[numbers.length][];
    for (int i = 0; i < numbers.length; i++) {
        Number[] row = numbers[i];
        result[i] = new String[row.length];
        for (int j = 0; j < row.length; j++) {
            result[i][j] = row[j] == null ? null : row[j].toString();
        }
    }
    return result;
}

不太明显的方法:

public static String[][] convert(Number[][] numbers) {
    return Arrays.stream(numbers)
            .map(row -> Arrays.stream(row).map(n -> String.valueOf(n)).toArray(String[]::new))
            .toArray(String[][]::new);
}
英文:

The most obvious way:

public static String[][] convert(Number[][] numbers) {
        String[][] result = new String[numbers.length][];
        for (int i = 0; i &lt; numbers.length; i ++) {
            Number[] row = numbers[i];
            result[i] = new String[row.length];
            for(int j = 0; j &lt; row.length; j ++) {
                result[i][j] = row[j] == null ? null : row[j].toString();
            }
        }
        return result;
    }

Less obvious way:

public static String[][] convert(Number[][] numbers) {
        return Arrays.stream(numbers)
                .map(row -&gt; Arrays.stream(row).map(n -&gt; String.valueOf(n)).toArray(String[]::new))
                .toArray(String[][]::new);
    }

huangapple
  • 本文由 发表于 2020年10月14日 20:09:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64352934.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定