gpt4 book ai didi

java - 关于 "for"语句中迭代器对象的问题

转载 作者:行者123 更新时间:2023-12-02 09:09:33 25 4
gpt4 key购买 nike

我试图理解一个java程序,它有一个代码段,例如

for (final Document document : cluster.getDocuments())  {  
if (documentsShown >= maxNumberOfDocumentsToShow) {
break;
}
displayDocument(level + 1, document);
documentsShown++;
}

这是否意味着 cluster.getDocuments() 必须是迭代器对象?

最佳答案

它必须是一个可迭代对象(即集合或数组)。基本上,

It's commonly used to iterate over an array or a Collections class (eg, ArrayList). It can also iterate over anything that implements the Iterable interface (must define iterator() method). Many of the Collections classes (eg, ArrayList) implement Iterable, which makes the for-each loop very useful. You can also implement Iterable for your own data structures.

Reference

All Known Implementing Classes of Iterable

关于java - 关于 "for"语句中迭代器对象的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6231122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com