Iterator
An iterator is a design pattern, which is an object that can traverse and select objects in a sequence, and developers do not need to understand the underlying structure of the sequence. An iterator is often called a "lightweight" object because it is cheap to create it.
The Iterator function in Java is relatively simple and can only be moved in one direction:
(1) Use method iterator() requires the container to return an Iterator. The first time the next() method of Iterator is called, it returns the first element of the sequence. Note: the iterator() method is the java.lang.Iterable interface and is inherited by the Collection.
(2) Use next() to obtain the next element in the sequence.
(3) Use hasNext() to check whether there are still elements in the sequence.
(4) Use remove() to delete the newly returned element of the iterator.
Iterator is the simplest implementation of Java iterator. ListIterator designed for List has more features, it can traverse List from two directions, or insert and delete elements from 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);}/*Iterator is used for while loop Iterator iter = l.iterator(); while(iter.hasNext()){String str = (String) iter.next();System.out.println(str);}*/The above is the usage of Iterator iterator in Java introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!