在 Java 中迭代字符串列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4028234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 08:42:03  来源:igfitidea点击:

Iterating over a List of Strings In Java?

javalistcsviterator

提问by Elliot Smith

I am using OpenCSVto read data from a CSV file and am using some of the sample code from the homepage:

我正在使用OpenCSV从 CSV 文件中读取数据,并使用主页中的一些示例代码:

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.

任何人都可以向我解释我应该如何做到这一点,因为我似乎无法解决。

回答by skaz

Can't you do: 1)

你不能这样做:1)

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

or

或者

2)

2)

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

回答by HED

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

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

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

回答by ColinD

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

CSVReader如果可能,您应该使用泛型。readAll()实际上返回 aList<String[]>而不是 a List<String>。然后,您只需要:

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

回答by kaliatech

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);
        }
    }

回答by Buhake Sindi

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`
        }
    }
}

回答by Peter Szanto

In Java 8 and above you can also do

在 Java 8 及更高版本中,您还可以执行

    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)

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