【问题标题】:How do I add an item to a String array of type List?如何将项目添加到 List 类型的 String 数组?
【发布时间】:2017-04-16 20:52:51
【问题描述】:

在我的代码中,我正在使用 CSVReader 读取文本文件并将所有内容放入列表类型的字符串数组中:

CSVReader reader = new CSVReader(new FileReader("addTest.txt"), ',');
List<String[]> myEntries = reader.readAll();
reader.close();

我现在想将一个项目添加到列表中,但是,当尝试通过myEntries.add("test" + "," + "test2") 执行此操作时,它不接受它。我的目标是将一个或多个项目添加到新行,然后我将使用 CSVWriter 将其写回文件。

如何将一个或多个项目添加到列表类型的字符串数组中?我环顾四周,但这个问题仍然困扰着我。

谢谢。

【问题讨论】:

标签: java arrays list csv opencsv


【解决方案1】:

List 采用字符串数组而不是逗号分隔的字符串。可以添加为

myEntries.add (new String [] {"test", "test2"});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-26
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    • 2021-08-05
    • 2016-09-28
    相关资源
    最近更新 更多