问题描述
我被要求在我的编码中使用增强的 for
循环.
I have been asked to use the enhanced for
loop in my coding.
我只学过如何使用传统的 for
循环,因此不知道它与增强的 for
循环之间的区别.
I have only been taught how to use traditional for
loops, and as such don't know about the differences between it and the enhanced for
loop.
增强的 for
循环与 Java 中传统的 for
循环有何不同?
How does an enhanced for
loop differ from a traditional for
loop in Java?
我应该注意哪些教程往往不提及的任何错综复杂的地方?
Are there any intricacies that I should look out for which tutorials tend not to mention?
推荐答案
增强的 for 循环:
Enhanced for loop:
for (String element : array) {
// rest of code handling current element
}
传统的 for 循环等效:
Traditional for loop equivalent:
for (int i=0; i < array.length; i++) {
String element = array[i];
// rest of code handling current element
}
看看这些论坛:https://blogs.oracle.com/CoreJavaTechTips/entry/using_enhanced_for_loops_with
http://www.java-tips.org/java-se-tips/java.lang/the-enhanced-for-loop.html
这篇关于Java 中增强的 for 循环的语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!