I have been asked to use the enhanced for
loop in my coding.
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.
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?
Enhanced for loop:
for (String element : array) {
// rest of code handling current element
}
Traditional for loop equivalent:
for (int i=0; i < array.length; i++) {
String element = array[i];
// rest of code handling current element
}
Take a look at these forums: 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