运行此代码时,迭代器iter
在我第二次使用时为空。我每次在外循环中实例化iter
,但这无济于事!也就是说,在第一次迭代之后,可迭代的长度为零!
为什么会这样?在这种情况下,我缺少什么常规做法?
String path = "somefile.csv";
try {
Reader in = new FileReader(path);
Iterable<CSVRecord> records = CSVFormat.RFC4180.withHeader(MetadataExtractorForTables.HeaderType.class)
.withDelimiter('\t').parse(in);
// A stupid loop
for(int i = 0; i < 3; i++) {
Iterator<CSVRecord> iter = records.iterator();
System.out.println("---> " + Iterators.size(iter));
System.out.println("--> " + StreamSupport.stream(records.spliterator(), false).count());
/* ----------------------------------------
* here I would like to use the iterator
* in a (nested) loop
* ---------------------------------------- */
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
输出看起来像这样:
---> 191
--> 0
---> 0
--> 0
---> 0
--> 0