definition: an element that is used sequentially to access a collection object without knowing the underlying representation of the collection object.
Characteristics:
1. It supports traversing an aggregation object in a different way.
2, the iterator simplifies the aggregation class.
3. There can be multiple traversal on the same aggregation.
4, in the iterator mode, add new aggregation class and iterator class is very convenient, no need to modify the original code.
Enterprise-class development and applications in common frameworks: Java collections implement Iterators
Concrete Examples:
public class Demo {public static void main (string[] args) {Actualcontainer containe
R = new Actualcontainer ();
for (int i = 5; i < i++) {container.add (i);
} Iterator iterator = Container.iterator ();
while (Iterator.hasnext ()) {System.out.println (Iterator.next ());
}}/** * Iterator interface, contains a common iterator method * * Interface iterator{public boolean hasnext ();
Public Object next ();
/** * Container interface: Contains a method to obtain an iterator/interface container{public iterator iterator (); /** * Specific Implementation class * @author Jiaoyuyu * */class Actualcontainer implements container{private list<object> List = NE
W arraylist<> ();
public void Add (Object obj) {this.list.add (obj);
public void Remove (Object obj) {this.list.remove (obj);
Public Object get (int index) {if (Index <= (this.list.size ()-1)) {return this.list.get (index);
return null;
Public iterator iterator () {return new actualiterator (); } Private Class ActualiteratoR implements iterator{private int pointer = 0;
public Boolean Hasnext () {return This.pointer < list.size () True:false;
Public Object Next () {if (This.pointer < List.size ()) {Object obj = list.get (pointer);
pointer++;
return obj;
return null; }
}
}
The iterator pattern belongs to a relatively simple pattern, which is mainly used to traverse objects of a collection class.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.