在 Java 中迭代字符串列表?

发布于 2024-09-30 00:30:39 字数 332 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

空城旧梦 2024-10-07 00:30:39

假设您要将每行的每个条目输出到它自己的行:

    List<String[]> myEntries = reader.readAll();
    for (String[] lineTokens : myEntries) {
        for (String token : lineTokens) {
            System.out.println(token);
        }
    }

Assuming you want to output each entry of each line to it's own line:

    List<String[]> myEntries = reader.readAll();
    for (String[] lineTokens : myEntries) {
        for (String token : lineTokens) {
            System.out.println(token);
        }
    }
心头的小情儿 2024-10-07 00:30:39

你不能这样做:
1)

for(int i = 0; i < myEntries.size(); i++){
  myEntries.get(i); // do something else here
}

2)

for(String s: myEntries)
   // Do work on s here

Can't you do:
1)

for(int i = 0; i < myEntries.size(); i++){
  myEntries.get(i); // do something else here
}

or

2)

for(String s: myEntries)
   // Do work on s here
仅此而已 2024-10-07 00:30:39

您尝试过使用迭代器吗?
它应该是这样的:

Iterator it = myEntries.iterator();
while(it.hasNext()){
  System.out.println(it.next());
}

Have you tried using an Iterator?
It should be something like this:

Iterator it = myEntries.iterator();
while(it.hasNext()){
  System.out.println(it.next());
}
镜花水月 2024-10-07 00:30:39

如果可能的话,您应该将泛型与 CSVReader 一起使用。 readAll() 实际上返回一个 List 而不是 List。然后,您只需要:

for (String[] row : myEntries) {
  System.out.println(Arrays.toString(row));
}

You should be using generics with CSVReader if possible. readAll() actually returns a List<String[]> and not a List<String>. Then, you just need to:

for (String[] row : myEntries) {
  System.out.println(Arrays.toString(row));
}
甜柠檬 2024-10-07 00:30:39

你可以做一些这样的事情。我在迭代器上添加了同步。

List<String[]> myEntries = reader.readAll();
Iterator<String[]> iter = myEntries.iterator();
synchronized (iter) {
    while (iter.hasNext()) {
        String[] items = iter.next();

        for (String item: items) { //foreach loop
            //Work with `item`
        }
    }
}

You can do something of this effect. I've added synchronization on iterator.

List<String[]> myEntries = reader.readAll();
Iterator<String[]> iter = myEntries.iterator();
synchronized (iter) {
    while (iter.hasNext()) {
        String[] items = iter.next();

        for (String item: items) { //foreach loop
            //Work with `item`
        }
    }
}
長街聽風 2024-10-07 00:30:39

在Java 8及以上版本中,您也可以

    List<String> s = new LinkedList<>();
    s.stream().forEach(System.out::print);

使用paralleStream()或

    List<String> s = new LinkedList<>();
    s.stream().forEach(s ->
            System.out.print(s)
    );

stream()来进行并行执行。互联网上有很多关于哪个执行速度更快的文章,答案是:“这取决于”(主要取决于列表中的项目数量)

In Java 8 and above you can also do

    List<String> s = new LinkedList<>();
    s.stream().forEach(System.out::print);

or

    List<String> s = new LinkedList<>();
    s.stream().forEach(s ->
            System.out.print(s)
    );

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文