当我运行这段代码时,迭代器
iter
iter
每次都在外循环,但都没用!也就是说,在第一次迭代之后,iterable的长度为零!
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