[Java class set] _ iterator interface notes
Objectives of this chapter:
Master the standard operations of set output
Master the main functions and precautions of the iterator Interface
The following methods are supported in set operations:
Iterator
Listiterator
Foreach output
Enumeration output
3. Details
Iterator interface Introduction
Standard Operations for set output:
"When using set output, you must use the iterator interface as long as you encounter a set output operation. This is the most standard practice. "
Operation Principle of the iterator interface:
Iterator is a specialized iterative output interface. The so-called iterative output is to judge the elements one by one to determine whether there is any content. If there is any content, the content is taken out.
For iterator, because it is an interface, you must rely on the collection interface to instantiate it.
Iterator <E> iterator ()
Import Java. util. iterator; import Java. util. list; import Java. util. arraylist; public class iteratordemo01 {public static void main (string [] ARGs) {list <string> alllist = new arraylist <string> (); alllist. add ("hello"); alllist. add ("_"); alllist. add ("world !!! "); Iterator <string> iter = All. iterator (); While (ITER. hasnext () {// determine whether there is content system. out. println (ITER. next (); // output content }}}
Output:
Hello
_
World !!!
The above operations are the standard operations of iterator
The remove () method is provided in the iterator interface. The function of this method is to delete the current object.
Import Java. util. list; import Java. util. iterator; import Java. util. arraylist; public class iteratordemo02 {public static void main (string [] ARGs) {list <string> All = new arraylist <string> (); All. add ("hello"); All. add ("_"); All. add ("world !!! "); Iterator <string> iter = All. iterator (); While (ITER. hasnext () {string STR = ITER. next (); If ("_". equals (STR) {ITER. remove (); // Delete element} else {system. out. println (STR); // output content} system. out. println ("deleted collection:" + ALL );}}
Output:
Hello
World !!!
The deleted collection: [Hello, world!]
In fact, iterator rarely calls the delete operation because its function is to output content. Note the following when deleting an object:
The list interface itself has the deletion method: Remove ()
If you use remove () in the list to perform the delete operation during iteration output, the code will be faulty.
Import Java. util. list; import Java. util. iterator; import Java. util. arraylist; public class iteratordemo02 {public static void main (string [] ARGs) {list <string> All = new arraylist <string> (); All. add ("hello"); All. add ("_"); All. add ("world !!! "); Iterator <string> iter = All. iterator (); While (ITER. hasnext () {string STR = ITER. next (); If ("_". equals (STR) {All. remove (); // Delete element} else {system. out. println (STR); // output content} system. out. println ("deleted collection:" + ALL );}}
Output:
Iteratordemo02.java: 14: The symbol cannot be found.
Symbol: Remove ()
Location: interface java. util. List <java. Lang. String>
All. Remove (); // delete an element
^
1 error
4. Summary
The iterator interface is used for frontend and backward output and is a unidirectional output.
The main function of iterator is to complete iterative output operations.
It is best not to delete data when using iterator.