简体   繁体   English

Java字符串分割数

[英]Java string split numbers

I have some rows of numbers in String str ; 我在String str有一排数字;
Numbers split by TAB 按TAB拆分数字

1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
\\... many others, ~ 2 000 strings

I need split columns with 我需要用

1 numbers to massive1, 1个数字为mass1,
2 numbers to massive2, 2等于mass2,
3 numbers to massive3, 3等于mass3,
4 numbers to massive4, 4个数字为mass4,
5 numbers to massive5, 5个数字到大量5,
6 numbers to massive6 6个数字到大量6

I know how to solve this task with many for / while loops but I need compact solve for this task. 我知道如何使用许多for / while循环解决此任务,但我需要紧凑解决此任务。 Maybe I need Patern.compile or String.split ? 也许我需要Patern.compileString.split吗?

Some of my code: 我的一些代码:

for (i = 0; i <= fileLen; i++) {
  while (s.charAt(i) != 13 && s.charAt(i + 1) != 10) {

    while (s.charAt(i) != 9) {
      n1.add((int) s.charAt(i));
      i++;
    }
    // skip TAB
    i++;
    // next for other column

Here you go, 干得好,

    String str = "1 2 3 4 5 6 \n 1 2 3 4 5 6 \n 1 2 3 4 5 6";
    Arrays.asList(str.split("\n")).stream().map((s) -> s.split(" ")).map((splitBySpace) -> {
        for (String sp : splitBySpace) {
                System.out.print(sp);
            }
            return splitBySpace;
        }).forEachOrdered((String[] _item) -> {
            System.out.println();
    });

---Output--- -输出-
123456 123456
123456 123456
123456 123456

Instead of variables massive1, ..., massive6 a variable lenght List massives would be more suitable: 而不是massive1, ..., massive6不是变量长度List massives massive1, ..., massive6会更合适:

List<List<Integer>> massives = Arrays.stream(str.split("\\R"))  // Stream<String>
    .map(line -> Arrays.stream(line.split("\t"))                  //   Stream<String>
         .map(field -> Integer::valueOf)                          //   Stream<Integer>
         .collect(Collectors.toList()))                           //   List<Integer>
     .collect(Collectors.toList());                             // List<List<Integer>>

List<int[]> massives = Arrays.stream(str.split("\\R"))
     .map(line -> Arrays.stream(line.split("\t"))
         .mapToInt(Integer::parseInt)
         .toArray())
     .collect(Collectors.toList());

Maybe with: 也许与:

massive1 = massives.get(0);
massive2 = massives.get(1);
massive3 = massives.get(2);
massive4 = massives.get(3);
...

Explanation: 说明:

  • String[] String#split(String regex) would split using the line-break match ( \\\\R ) into several lines. String[] String#split(String regex)将使用换行符( \\\\R )分成几行。
  • Stream.of / Arrays.stream turns String[] into Stream<String> a kind of iteration through every String. Stream.of / Arrays.streamString[]转换为Stream<String>这是对每个String的迭代。
  • Stream.map turns turns every String when using Integer::valueOf into an Integer. 使用Integer :: valueOf时, Stream.map每个String转换为Integer。
  • Stream.collect collects every Integer into a List. Stream.collect将每个Integer收集到一个List中。

Streams are very expressive, but might not be suited for total beginners, as they combine all into one single expression, which may easily cause errors. 流具有很高的表现力,但可能不适合初学者使用,因为它们将全部组合成一个表达式,很容易引起错误。


After understanding the question: 了解问题后:

int[][] rows = Stream.of(str.split("\\R"))
    .map(line -> Stream.of(line.split("\\s+"))
        .mapToInt(Integer.parseInt)
        .toArray())
    .toArray(int[][]::new);

However one wants the columns: 但是,有人想要这些列:

int m = rows.length;
int n = Arrays.stream(rows).mapToInt(line -> line.length).min().orElse(0);

int[][] columns = IntStream.range(0, n)
    .mapToObj(j -> IntStream.range(0, m)
        .map(i -> rows[i][j])
        .toArray()).toArray(int[][]::new);

System.out.println(Arrays.deepToString(columns));

Actually I advise to convert rows to columns by classical for-loops; 实际上,我建议通过经典的for循环将行转换为列。 much better readable. 可读性更好。

But a StackOverflow answer should not necessarily pick the easiest way. 但是,StackOverflow答案不一定必须选择最简单的方法。

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

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