Easy to master Java iterator pattern _java

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.