在java调用方法中的每个循环都进行了高级的操作,该方法返回一个数组来遍历每一次? [英] does advanced for each loop in java call method that returns an array to iterate over every single time?

查看:278
本文介绍了在java调用方法中的每个循环都进行了高级的操作,该方法返回一个数组来遍历每一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他,



我有一个问题,先进的循环在Java中。如果有一个方法调用Method.returnArray(),我迭代数组与
(ArrayElement e:Method.returnArray()){
//做不到
}



是否会每次迭代调用.returnArray()?
$ b

谢谢。
<解决方案不是,它不会。第一次调用的结果将被存储在一个临时变量的编译代码中。



From Effective Java 2nd。第46项:


请注意,使用
for-each循环没有性能损失,即使是阵列。事实上,在某些情况下,它可能会提供一个比普通for循环略微的性能优势
,因为它只计算一次数组索引
的限制。


His,

I have a question to the advanced for loop in java. If there is a method call Method.returnArray() and I iterate over the array with for (ArrayElement e : Method.returnArray()) { //do smth }

will the .returnArray() be called by every iteration?

Thanks.

解决方案

No, it won't. The result of the first call will be stored in the compiled code in a temporary variable.

From Effective Java 2nd. Ed., Item 46:

Note that there is no performance penalty for using the for-each loop, even for arrays. In fact, it may offer a slight performance advantage over an ordinary for loop in some circumstances, as it computes the limit of the array index only once.

这篇关于在java调用方法中的每个循环都进行了高级的操作,该方法返回一个数组来遍历每一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆