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+