The iterator has been used many times during this time, but I am not very familiar with this "tool". Here is a simple introduction.
An iterator 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. Note: The iterator () method is a Java. Lang. iterable interface inherited by collection.
(2) Use next () to obtain the next element in the sequence. When the next () method of iterator is called for the first time, it returns the first element of the sequence. (Note)
(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.
The following are two simple iterator application instances:
// Used for the for loop list = new arraylist (); list. add ("user1"); list. add ("user2"); list. add ("user3"); For (iterator iter = List. iterator (); ITER. hasnext ();) {string STR = (string) ITER. next (); system. out. println (STR);} // iterator used for the while loop list = new arraylist (); list. add ("user1"); list. add ("user2"); list. add ("user3"); iterator iter = List. iterator (); While (ITER. hasnext () {string STR = (string) ITER. next (); system. out. println (STR );}
As a matter of fact, I am not familiar with iterator. I always feel that I am not familiar with it. After understanding these knowledge queries, we can say that they are "Knowledgeable". Naturally, they are more confident to use them.