Java如何使用迭代器?
通常情況下,想循環在集合中的元素。例如,可能希望顯示的每個元素。
做到這一點最簡單的方法是使用一個迭代器,它是一個對象,它實現無論是迭代器或實現ListIterator接口。
迭代器,使能夠循環通過收集,獲取或移除元素。ListIterator擴展迭代器允許列表的雙向遍曆和元素的修改。
之前,可以通過一個迭代器訪問一個集合。每個集合類提供了一個iterator()方法,該方法返回一個迭代器集合的開始。通過使用這個迭代器對象,可以在同一時間訪問集合,一個元素中的每個元素。
一般情況下,通過一個集合的內容使用迭代循環,請按照下列步驟操作:
-
通過調用集合的iterator()方法獲得一個迭代器集合的開始。
-
建立一個循環,使一個調用的hasNext()。有循環迭代隻要hasNext()返回true。
-
在循環中,通過調用next()方法獲得的每個元素。
對於實現List集合,也可以通過調用的ListIterator得到一個迭代器。
通過迭代器聲明的方法:
SN | 方法及描述 |
---|---|
1 |
boolean hasNext( ) Returns true if there are more elements. Otherwise, returns false. |
2 |
Object next( ) Returns the next element. Throws NoSuchElementException if there is not a next element. |
3 |
void remove( ) Removes the current element. Throws IllegalStateException if an attempt is made to call remove( ) that is not preceded by a call to next( ). |
ListIterator所聲明的方法:
SN | 方法及描述 |
---|---|
1 |
void add(Object obj) Inserts obj into the list in front of the element that will be returned by the next call to next( ). |
2 |
boolean hasNext( ) Returns true if there is a next element. Otherwise, returns false. |
3 |
boolean hasPrevious( ) Returns true if there is a previous element. Otherwise, returns false. |
4 |
Object next( ) Returns the next element. A NoSuchElementException is thrown if there is not a next element. |
5 |
int nextIndex( ) Returns the index of the next element. If there is not a next element, returns the size of the list. |
6 |
Object previous( ) Returns the previous element. A NoSuchElementException is thrown if there is not a previous element. |
7 |
int previousIndex( ) Returns the index of the previous element. If there is not a previous element, returns -1. |
8 |
void remove( ) Removes the current element from the list. An IllegalStateException is thrown if remove( ) is called before next( ) or previous( ) is invoked. |
9 |
void set(Object obj) Assigns obj to the current element. This is the element last returned by a call to either next( ) or previous( ). |
例子:
下麵是一個例子證明這兩個迭代器和ListIterator。它使用一個ArrayList對象,但是一般原則適用於任何類型的集合。
當然,ListIterator隻提供給那些實現了List接口的集合。
import java.util.*; public class IteratorDemo { public static void main(String args[]) { // Create an array list ArrayList al = new ArrayList(); // add elements to the array list al.add("C"); al.add("A"); al.add("E"); al.add("B"); al.add("D"); al.add("F"); // Use iterator to display contents of al System.out.print("Original contents of al: "); Iterator itr = al.iterator(); while(itr.hasNext()) { Object element = itr.next(); System.out.print(element + " "); } System.out.println(); // Modify objects being iterated ListIterator litr = al.listIterator(); while(litr.hasNext()) { Object element = litr.next(); litr.set(element + "+"); } System.out.print("Modified contents of al: "); itr = al.iterator(); while(itr.hasNext()) { Object element = itr.next(); System.out.print(element + " "); } System.out.println(); // Now, display the list backwards System.out.print("Modified list backwards: "); while(litr.hasPrevious()) { Object element = litr.previous(); System.out.print(element + " "); } System.out.println(); } }
這將產生以下結果:
Original contents of al: C A E B D F Modified contents of al: C+ A+ E+ B+ D+ F+ Modified list backwards: F+ D+ B+ E+ A+ C+