将多条记录添加到List<List<Object>>中。

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

Add multiple records to List<List<Object>>

问题

  1. 我正在尝试向对象的列表列表中添加多个记录但我不知道该如何做```java```)。
  2. 唯一我发现可能的方法就是直接这样做
  3. List&lt;List&lt;Object&gt;&gt; values = Arrays.asList(
  4. Arrays.asList(&quot;84935&quot;, &quot;01/02/2020&quot;, &quot;01/02/2020&quot;, &quot;已解决&quot;, &quot;XXX&quot;, &quot;XXX&quot;, &quot;XXX&quot;, &quot;XXX&quot;, &quot;1&quot;, &quot;1&quot;, &quot;15&quot;, &quot;0&quot;, &quot;2020&quot;),
  5. Arrays.asList(&quot;84936&quot;, &quot;02/02/2020&quot;, &quot;02/02/2020&quot;, &quot;已解决&quot;, &quot;XXX&quot;, &quot;XXX&quot;, &quot;XXX&quot;, &quot;XXX&quot;, &quot;1&quot;, &quot;1&quot;, &quot;15&quot;, &quot;0&quot;, &quot;2020&quot;),
  6. Arrays.asList(&quot;84937&quot;, &quot;03/02/2020&quot;, &quot;03/02/2020&quot;, &quot;已解决&quot;, &quot;XXX&quot;, &quot;XXX&quot;, &quot;XXX&quot;, &quot;XXX&quot;, &quot;1&quot;, &quot;1&quot;, &quot;15&quot;, &quot;0&quot;, &quot;2020&quot;)
  7. );
  8. 我想读取一个 ```.csv``` 文件并将其写入该列表当我尝试使用 ```.add``` ```set``` 我会得到一个空指针异常我还没有找到解决方法
  9. 是否可以使用循环自动添加记录由于这是唯一一种 Google Sheets Api 允许的方式所以列表必须是 ```List&lt;List&lt;Object&gt;&gt;```
  10. 谢谢
英文:

I'm trying to add multiple records in a List of List of Objects and I don't know how to do it (java).

The only way I've found it possible is by doing it literally:

  1. List&lt;List&lt;Object&gt;&gt; values = Arrays.asList(
  2. Arrays.asList(&quot;84935&quot;, &quot;01/02/2020&quot;, &quot;01/02/2020&quot;, &quot;resolved&quot;, &quot;XXX&quot;, &quot;XXX&quot;, &quot;XXX&quot;, &quot;XXX&quot;, &quot;1&quot;, &quot;1&quot;, &quot;15&quot;, &quot;0&quot;, &quot;2020&quot;),
  3. Arrays.asList(&quot;84936&quot;, &quot;02/02/2020&quot;, &quot;02/02/2020&quot;, &quot;resolved&quot;, &quot;XXX&quot;, &quot;XXX&quot;, &quot;XXX&quot;, &quot;XXX&quot;, &quot;1&quot;, &quot;1&quot;, &quot;15&quot;, &quot;0&quot;, &quot;2020&quot;),
  4. Arrays.asList(&quot;84937&quot;, &quot;03/02/2020&quot;, &quot;03/02/2020&quot;, &quot;resolved&quot;, &quot;XXX&quot;, &quot;XXX&quot;, &quot;XXX&quot;, &quot;XXX&quot;, &quot;1&quot;, &quot;1&quot;, &quot;15&quot;, &quot;0&quot;, &quot;2020&quot;)
  5. );

I want to read a .csv file and write in it that list. When I try with .add or set I get a nullpointer Exception and I haven't been able to find a solution.

Is it possible to add the records automatically with a loop? It is mandatory that the List is List&lt;List&lt;Object&gt;&gt; because it's the only way that Google Sheets Api allows.

Thank you.

答案1

得分: 2

是的,完全可以在循环内部编写列表的列表:

例如:

  1. List<List<String>> list = new ArrayList<>();
  2. for (String line : lines) {
  3. list.add(Arrays.asList(line.split(",")));
  4. }

如果你正在读取CSV文件,最好使用第三方库,比如Apache CSVParser。

然而,如果你坚持自己做:

  1. List<List<String>> fields = Files.lines(fileName)
  2. .map(line -> Arrays.asList(line.split(",")))
  3. .collect(Collectors.toList());
英文:

Yes it's absolutely possible to write a list of lists inside a loop:

For example:

  1. List&lt;List&lt;String&gt;&gt; list = new ArrayList&lt;&gt;();
  2. for (String line: lines) {
  3. list.add(Arrays.asList(line.split(&quot;,&quot;));
  4. }

If you're reading a CSV file you'd be better off using a third party library such as Apache CSVParser.

However if you insist on doing it yourself:

  1. List&lt;List&lt;String&gt;&gt; fields = Files.lines(fileName)
  2. .map(l -&gt; Arrays.asList(l.split(&quot;,&quot;)))
  3. .collect(Collector.toList());

huangapple
  • 本文由 发表于 2020年10月7日 07:11:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64235002.html
匿名

发表评论

匿名网友

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

确定