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) {
// Do something with element of type String
System.out.println(element);
}
Traditional for loop: (
for (int i=0; i < array.length; i++) {
// Reference to the item in array[position]
String element = array[i];
// More code using String element
}