将3位数字字符串读取到ArrayList中。

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

Reading 3 digits string numbers to an ArrayList?

问题

I get a line in the following block:

while ((line = reader.readLine()) != null) {
    System.out.println(line); // 000,050,007
    List<Integer> list = List.of(Integer.parseInt(line))
}

However, I cannot cast the 3 digit values (string) to Integer. So, how can I do this in an elegant way?

Update: I use the following approach, but I am not sure if there is a better way than using map() 2 times?

List<Integer> items= Stream.of(line.split(","))
                        .map(String::trim)
                        .map(Integer::parseInt)
                        .toList();
英文:

I am reading values from a text files as shown below:

000,050,007
059,000,157
002,038,000

I get a line in the following block:

while ((line = reader.readLine()) != null) {
    System.out.println(line); // 000,050,007
    List<Integer> list = List.of(Integer.parseInt(line))
}

However, I cannot cast the 3 digit values (string) to Integer. So, how can I do this in an elegant way?

Update: I use the following approach, but I am not sure if there is a better way than using map() 2 times?

List<Integer> items= Stream.of(line.split(","))
                        .map(String::trim)
                        .map(Integer::parseInt)
                        .toList();

答案1

得分: 2

以下是翻译好的代码部分:

一个 `Scanner` 也是可能的

    import java.util.List;
    import java.util.Scanner;
    import java.nio.file.Files;
    import java.nio.file.Path;
    
    public class NScan {
        public static void main(String[] args) {
            try (Scanner s = new Scanner(Files.newBufferedReader(Path.of(args[0]))).useDelimiter("[\r\n,]")) {
                List<Integer> ints = s.tokens()
                    .map(String::trim)
                    .map(Integer::valueOf)
                    .toList();
                System.out.println(ints);
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    }
英文:

A Scanner is possible too:

import java.util.List;
import java.util.Scanner;
import java.nio.file.Files;
import java.nio.file.Path;

public class NScan {
    public static void main(String[] args) {
        try (Scanner s = new Scanner(Files.newBufferedReader(Path.of(args[0]))).useDelimiter(&quot;[\r\n,]&quot;)) {
            List&lt;Integer&gt; ints = s.tokens().
                map(String::trim).
                map(Integer::valueOf).
                toList();
            System.out.println(ints);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}

答案2

得分: 0

在将其转换为整数列表之前,您需要在逗号上拆分该行。

List<List<Integer>> list = new ArrayList<>();
while ((line = reader.readLine()) != null) {
    System.out.println(line); // 000,050,007
    List<Integer> integers = Arrays.stream(line.split(","))
        .map(s -> Integer.parseInt(s.trim()))
        .toList();
    list.add(integers);
}

请注意,具有多个 map 调用不会影响性能,您只需根据可读性来决定。

由于您使用了 BufferedReader,您可以使用流替换 while 循环:

List<List<Integer>> list = reader.lines().map(line ->
    Arrays.stream(line.split(","))
        .map(s -> Integer.parseInt(s.trim()))
        .toList()
).toList();
英文:

You need to split the line on comma before converting it to List of integers

List&lt;List&lt;Integer&gt;&gt; list = new ArrayList&lt;&gt;();
while ((line = reader.readLine()) != null) {
    System.out.println(line); // 000,050,007
    List&lt;Integer&gt; integers = Arrays.stream(line.split(&quot;,&quot;))
        .map(s -&gt; Integer.parseInt(s.trim()))
        .toList();
    list.add(integers);
}

Note that having multiple map calls doesn't affect the performance, you just need to decide based on readability here.

As you are using BufferedReader, you can replace while loop with stream

List&lt;List&lt;Integer&gt;&gt; list = reader.lines().map(line -&gt;
    Arrays.stream(line.split(&quot;,&quot;))
        .map(s -&gt; Integer.parseInt(s.trim()))
        .toList()
).toList();

huangapple
  • 本文由 发表于 2023年4月6日 19:40:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949102.html
匿名

发表评论

匿名网友

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

确定