java:集合表达式在“ foreach”中被计算了多少次? [英] java: how many times is the collection expression evaluated in a "foreach"
本文介绍了java:集合表达式在“ foreach”中被计算了多少次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我在Java中这样做:
if I do this in Java:
for(String s : myCollection.expensiveListGeneration())
{
doSomething();
}
是否在开始或每个循环迭代中仅调用一次costListGeneration()?
is expensiveListGeneration() invoked just once at the beggining or in every cycle iteration?
它是否依赖于实现?
推荐答案
,因为它等同于使用迭代器,等效于调用collections。 iterator()方法,并且调用一次。
because it is equivalent to using an iterator, it is equivalent to calling the collections' . iterator() method, and it is called once.
这篇关于java:集合表达式在“ foreach”中被计算了多少次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文