Introduction of iterator model of Java design pattern _java

Source: Internet
Author: User
Tags int size
1. First define a container collection interface.
Copy Code code as follows:

Package com.njupt.zhb.learn.iterator;
Public interface Collection {
void Add (Object o);
int size ();
Iterator iterator ();
}



2. Defines an interface for a iterator iterator
Copy Code code as follows:

Package com.njupt.zhb.learn.iterator;
Public interface Iterator {
Object next ();
Boolean hasnext ();
}



3. Define a ArrayList, implement the collection interface, and write an inner class that implements the iterator interface.
Copy Code code as follows:

package com.njupt.zhb.learn.iterator;
Import com.njupt.zhb.learn.iterator.Collection;
public class ArrayList implements Collection {
 object[] objects = new OBJECT[10];
 int index = 0;
 public void Add (Object o) {
  if (index = = objects.length) {
   object[] Newob jects = new object[objects.length * 2];
   system.arraycopy (objects, 0, newobjects, 0, objects.length);
   objects = newobjects;
  }
  objects[index] = o;
  index + +;
 }

 public int size () {
  return index;
 }

 public Iterator iterator () {

  return new Arraylistiterator ();
 }

 private class Arraylistiterator implements iterator {
  private int currentindex = 0;
   @Override
 &nbSp;public Boolean hasnext () {
   if (currentindex >= index) return false;
   else return true;
  }
   @Override
  public Object Next () {
   object o = Objects[currentindex ];
   currentindex + +;
   return o;
  }

 }
}



4. Write test Procedures Testmain
Copy Code code as follows:

Package com.njupt.zhb.learn.iterator;
Import com.njupt.zhb.learn.iterator.ArrayList;
public class Testmain {
public static void Main (string[] args) {
Collection C = new ArrayList ();
for (int i=0; i<15; i++) {
C.add ("string" +i);
}
System.out.println (C.size ());
Iterator it = C.iterator ();
while (It.hasnext ()) {
Object o = It.next ();
System.out.println (o.tostring () + "");
}
}
}



Run Result:
Copy Code code as follows:

15
String 0
String 1
String 2
String 3
String 4
String 5
String 6
String 7
String 8
String 9
String 10
String 11
String 12
String 13
String 14



from the above we can see that design patterns are used everywhere in object-oriented polymorphism. interface to call a function in a child class. Click to download source code

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.