Tag: 每个

使用for循环还是for-each循环?

我们应该更喜欢for-each循环而不是传统的for循环吗? while循环是否有利? List 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 iter1 = names.iterator(); while (iter1.hasNext()) { log(iter1.next()); } //Iterator for loop for(Iterator iter2 = names.iterator(); iter2.hasNext();){ log(iter2.next()); } 什么是最好的味道?