在 Java 中迭代字符串列表?
我正在使用 OpenCSV 从 CSV 文件中读取数据,并使用主页中的一些示例代码
CSVReader reader = new CSVReader(new FileReader("stockInfo.csv"));
List myEntries = reader.readAll();
:我现在正在尝试循环浏览此列表并打印出每个条目。但我似乎无法弄清楚执行此操作的代码。
谁能向我解释一下我应该如何做到这一点,因为我似乎无法解决这个问题。
I am using OpenCSV to read data from a CSV file and am using some of the sample code from the homepage:
CSVReader reader = new CSVReader(new FileReader("stockInfo.csv"));
List myEntries = reader.readAll();
And i am now trying to loop through this list and print out each entry. But i cannot seem to figure out the code to perform this.
Could anyone explain to me how i am supposed to do this becuase i just cant seem to work it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
假设您要将每行的每个条目输出到它自己的行:
Assuming you want to output each entry of each line to it's own line:
你不能这样做:
1)
或
2)
Can't you do:
1)
or
2)
您尝试过使用迭代器吗?
它应该是这样的:
Have you tried using an Iterator?
It should be something like this:
如果可能的话,您应该将泛型与 CSVReader 一起使用。
readAll()
实际上返回一个List
而不是List
。然后,您只需要:You should be using generics with
CSVReader
if possible.readAll()
actually returns aList<String[]>
and not aList<String>
. Then, you just need to:你可以做一些这样的事情。我在迭代器上添加了同步。
You can do something of this effect. I've added synchronization on iterator.
在Java 8及以上版本中,您也可以
使用paralleStream()或
stream()来进行并行执行。互联网上有很多关于哪个执行速度更快的文章,答案是:“这取决于”(主要取决于列表中的项目数量)
In Java 8 and above you can also do
or
It is possible to use paralleStream() instead or stream() to make parallel execution. There are a lot of articles on the internet about which will execute quicker and the answer is : "it depends" (mostly on the number of items in the list)