Java语言中姐种遍历List的方法总结

时间:2022-10-20 20:15:23
遍历 List 的方法:
1. for
2. advanced for
3. Iterator
4. while
5. ListIterator List<E> list 1. for
for (int i = 0; i < list.size(); i++) {
E element = list.get(i);
} 2. advanced for
for (E element : list) { } 3. Iterator
for (Iterator<E> iter = list.iterator(); iter.hasNext(); ) {
E element = iter.next();
} 4. while
while (i < list.size()) {
E element = list.get(i);
i++;
} 5. ListIterator
for (ListIterator<E> iterator = list.listIterator(); iterator.hasNext(); ) {
E element = iterator.next();
}

  

Java遍历List四种方法的效率对比