我们应该更喜欢 for-each 循环而不是传统的 for-循环吗?while循环有优势吗?
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());
}
用什么味道最好?