Java Iterator (Iterator) general usage (conversion), javaiterator
Iterator)
An iterator is a design pattern. It is an object that can traverse and select objects in a sequence. Developers do not need to understand the underlying structure of the sequence. An iterator is usually called a "lightweight" object because it is easy to create.
The Iterator function in Java is relatively simple and can only be moved one way:
(1) The method iterator () requires the container to return an Iterator. When the next () method of Iterator is called for the first time, it returns the first element of the sequence. Note: The iterator () method is a java. lang. Iterable interface inherited by Collection.
(2) Use next () to obtain the next element in the sequence.
(3) Use hasNext () to check whether there are any elements in the sequence.
(4) use remove () to delete the elements returned by the iterator.
Iterator is the simplest implementation of the Java Iterator. The ListIterator designed for List has more functions. It can traverse the List in two directions, or insert and delete elements from the List.
Iterator application:
List l = new ArrayList ();
L. add ("aa ");
L. add ("bb ");
L. add ("cc ");
For (Iterator iter = l. iterator (); iter. hasNext ();){
String str = (String) iter. next ();
System. out. println (str );
}
/* The iterator is used for the while loop.
Iterator iter = l. iterator ();
While (iter. hasNext ()){
String str = (String) iter. next ();
System. out. println (str );
}
*/