在Java中迭代列表的方法
List<E> list
while / do while// Not recommended (see below)!for (int i = 0; i < list.size(); i++) {
E element = list.get(i);
// 1 - can call methods of element
// 2 - can use 'i' to make index-based calls to methods of list
// ...}ListgetIteratorLinkedList
ListArrayListgetLinkedList
Collections
for (E element : list) {
// 1 - can call methods of element
// ...}for (Iterator<E> iter = list.iterator(); iter.hasNext(); ) {
E element = iter.next();
// 1 - can call methods of element
// 2 - can use iter.remove() to remove the current element from the list
// ...}for (ListIterator<E> iter = list.listIterator(); iter.hasNext(); ) {
E element = iter.next();
// 1 - can call methods of element
// 2 - can use iter.remove() to remove the current element from the list
// 3 - can use iter.add(...) to insert a new element into the list
// between element and iter->next()
// 4 - can use iter.set(...) to replace the current element
// ...}list.stream().map(e -> e + 1); // Can apply a transformation function for e
IterableListforEach
如果有的话,还有什么其他的方法吗?
MMMHUHU
长风秋雁
相关分类