The following methods are supported in the collection.
Iterator
Listiterator
foreach Output
Emumeration output.
of the collection output
Standard Operation:
The collection output must form the following ideas: As long as the operation of the assembly output, you must use the iterator interface, which is the most important criteria .
of the Iterator interface
Operating principle:
Iterator is a specialized iterative output interface, so-called iterative output is to determine the elements of each one, to determine whether there is content, if there is content, then the content output.
for iterator, it is an excuse in itself, so to instantiate it needs to rely on the collection interface to complete .
You can see such a method in the collection interface:
Visible, the iterator interface can be called through the method iterator () inside the collection interface.
Therefore, all classes that inherit from collection have a iterator () method.
Standard method for instantiating iterator interfaces
list<e> all= new List Subclass <E> ();iterator<e> iter = All.iterator () ; // instantiating a iterator interface
Iterator's Own method:
Instance:
Packageclass set;Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.List; Public classtest1{ Public Static voidMain (String args[]) {List<String> all=NewArraylist<string> ();//All.add ("Hello") ; All.add ("_") ; All.add ("World") ; Iterator<String> iter = All.iterator ();//instantiating a iterator interface while(Iter.hasnext ()) {//determine if there is contentSystem.out.println (Iter.next ());//Output Content } }};
Output Result:
Hello_world
These are standard practices for iterator interfaces.
The Remove method is provided in the iterator interface, which is the ability to delete the current object.
Packageclass set;Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.List; Public classtest1{ Public Static voidMain (String args[]) {List<String> all=NewArraylist<string> ();//All.add ("Hello") ; All.add ("_") ; All.add ("World") ; Iterator<String> iter = All.iterator ();//instantiating a iterator interface while(Iter.hasnext ()) {//determine if there is contentString str =Iter.next (); if("_". Equals (str)) { iter.remove (); // Delete element }Else{System.out.println (str); //Output Content}} System.out.println ("The collection after deletion:" +All ); }};
Output Result:
HelloWorld the collection after deletion: [Hello, World]
This remove () method is seldom used in practical operations.
The list itself has the Remove () method.
If you use Remove () from list in the process of using the iteration output to perform a delete operation, the code will have a problem.
Packageclass set;Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.List; Public classtest1{ Public Static voidMain (String args[]) {List<String> all=NewArraylist<string> ();//All.add ("Hello") ; All.add ("_") ; All.add ("World") ; Iterator<String> iter = All.iterator ();//instantiating a iterator interface while(Iter.hasnext ()) {//determine if there is contentString str =Iter.next (); if("_". Equals (str)) { all.remove (str); // Delete element }Else{System.out.println (str); //Output Content}} System.out.println ("The collection after deletion:" +All ); }};
Output Result:
The collection after hello delete: [Hello, World]
As you can see here, once the list object executes the delete, the loop will stop executing.
When using Iteratror, do not use the Remove () method in the collection class and only use the methods in the iterator interface.
And the iterator is the output from the past, the unidirectional output,
The function is to complete the iterative output operation.
Iterator interface. Collection output