用Java从字符串构建一个锯齿数组

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

Build a jagged array from a String in Java

问题

我需要编写这个从字符串创建矩阵的代码

     * 字符串的格式如下
     *  - 矩阵的每一行由换行符 \n 分隔
     *  - 每行的元素由空格分隔
     *  例如字符串 "0 1\n2 3" 表示以下矩阵
          [0 1]
          [2 3]

在练习中矩阵可以是一个不规则数组”,以下是我的代码

    int[][] tab = null;
    int compteur = 0;
    int position = 0;
    for (int i = 0; i < s.length(); i++) {
        char l = s.charAt(i);
        String p = String.valueOf(l);
        if (Character.isDigit(s.charAt(i))) {
            tab[compteur][position] = s.charAt(i);
            position++;
        } else if (p.equals("\n")) {
            position = 0;
            compteur++;
        }
    }
    return tab;
英文:

I have to code this "create a matrix from a String:

 * The string if formatted as follow:
 *  - Each row of the matrix is separated by a newline
 *    character \n
 *  - Each element of the rows are separated by a space
 *  For example, the String &quot;0 1\n2 3&quot; represent the
 *  matrix:
      [0 1]
      [2 3]

In the exercise the matrix can be a "Jagged Array" and here is my code :

int[][] tab = null 
int compteur = 0
int position = 0
for (int i = 0; i &lt; s.length(); i++) {
    char l = s.charAt(i);
    String p = String.valueOf(l);
    if (Character.isDigit(s.charAt(i))) {
        tab[compteur][position] = s.charAt(i);
        position++;
    else if(p.equals(&quot;/n&quot;)) {
        position = 0;
        compteur ++;
    }
}
return tab

答案1

得分: 1

@Lionel Ding的解决方案当然有效,但值得一提的是,使用流可能会更加优雅。然而,逻辑仍将保持相同 - 通过\n字符拆分字符串,然后通过' '字符拆分每一行,将个别字符串解析为int并将结果收集到int[][]中:

private static int[][] toMatrix(String s) {
    return Arrays.stream(s.split("\n"))
                 .map(line -> Arrays.stream(line.split(" "))
                                    .mapToInt(Integer::parseInt).toArray())
                 .toArray(int[][]::new);
}
英文:

@Lionel Ding's solution will work, of course, but it's worth mentioning that using streams could arguably be more elegant. The logic would remain the same, however - you split the string by the \n character, and then split every line by the ' ' character, parse the individual strings to ints and collect the result to an int[][]:

private static int[][] toMatrix(String s) {
    return Arrays.stream(s.split(&quot;\n&quot;))
                 .map(line -&gt; Arrays.stream(line.split(&quot; &quot;))
                                    .mapToInt(Integer::parseInt).toArray())
                 .toArray(int[][]::new);
}

答案2

得分: 0

这是代码部分的翻译:

第一部分代码:

public static int[][] method(String s) {
    String[] x = s.split("\n");
    int[][] out = new int[x.length][x[0].split(" ").length];
    for (int i = 0; i < x.length; i++) {
        String[] y = x[i].split(" ");
        for (int j = 0; j < y.length; j++) {
            out[i][j] = Integer.parseInt(y[j]);
        }
    }
    return out;
}

输入:

String s = "0 1\n2 3\n4 5"

输出:

int[][] array = [0 1],[2 3],[4 5]

第二部分代码:

public static int[][] method(String s) {
    String[] x = s.split("\n");
    int max = Integer.MIN_VALUE;
    for (String string : x) {
        int l = string.split(" ").length;
        max = l > max ? l : max;
    }
    int[][] out = new int[x.length][max];
    for (int i = 0; i < x.length; i++) {
        String[] y = x[i].split(" ");
        for (int j = 0; j < y.length; j++) {
            out[i][j] = Integer.parseInt(y[j]);
        }
    }
    return out;
}

输入:

String s = "0 1\n2 3\n4 5 6\n1"

输出:

int[][] array = [0 1 0],[2 3 0],[4 5 6],[1 0 0]
英文:

Here's the code :

public static int[][] method(String s) {
    String[] x = s.split(&quot;\n&quot;);
    int[][] out = new int[x.length][x[0].split(&quot; &quot;).length];
    for (int i = 0; i &lt; x.length; i++) {
        String[] y = x[i].split(&quot; &quot;);
        for (int j = 0; j &lt; y.length; j++) {
            out[i][j] = Integer.parseInt(y[j]);
        }
    }
    return out;
}

IN :

String s = &quot;0 1\n2 3\n4 5&quot;

OUT :

int[][] array = [0 1],[2 3],[4 5]

This above code works fine for square matrix, but here the code that works for any kind of matrix ! :

    public static int[][] method(String s) {
    String[] x = s.split(&quot;\n&quot;);
    int max = Integer.MIN_VALUE;
    for (String string : x) {
        int l = string.split(&quot; &quot;).length;
        max = l &gt; max ? l : max;
    }
    int[][] out = new int[x.length][max];
    for (int i = 0; i &lt; x.length; i++) {
        String[] y = x[i].split(&quot; &quot;);
        for (int j = 0; j &lt; y.length; j++) {
            out[i][j] = Integer.parseInt(y[j]);
        }
    }
    return out;
}

IN :

String s = &quot;0 1\n2 3\n4 5 6\n1&quot;

OUT :

int[][] array = [0 1 0],[2 3 0],[4 5 6],[1 0 0]

huangapple
  • 本文由 发表于 2020年10月24日 19:36:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64512921.html
匿名

发表评论

匿名网友

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

确定