Should we prefer the for-each loop instead of the traditional for-loops? Is the while-loop advantageous?
List<String> names = Arrays.asList("John", "Jeff", "Mary", "Elise");
//for-each loop
for(String name: names){
log(name);
}
//traditional for-loop
for(int index=0; index < 10; ++index){
log(names.get(index));
}
//Iterator while
Iterator<String> iter1 = names.iterator();
while (iter1.hasNext()) {
log(iter1.next());
}
//Iterator for loop
for(Iterator<String> iter2 = names.iterator(); iter2.hasNext();){
log(iter2.next());
}
What is the best flavor to use?