Imycollection
package com.rain.Iterator;public interface IMyCollection<E> {int size();void add(E e);IMyIterator<E> iterator();}
Imyiterator
package com.rain.Iterator;public interface IMyIterator <E>{ boolean hasNext(); E next(); }
Myarraylist
Package COM. rain. iterator; public class myarraylist <E> implements imycollection <E> {object [] oldo = new object [10]; // container int Index = 0; // subscript @ overridepublic int size () {return this. index ;}@ overridepublic void add (E) {If (index> = oldo. length) {// if the container is full of object [] newo = new object [oldo. length * 2]; // create a new container to resize the system. arraycopy (oldo, 0, newo, 0, oldo. length); // copy the container data to the new container. oldo = newo; // point the reference to the new container} oldo [Index] = E; index ++ ;} @ overridepublic imyiterator <E> iterator () {return New myiterator ();} // internal class myiterator implements imyiterator <E> {int currentindex = 0; @ overridepublic Boolean hasnext () {If (this. currentindex <index) return true; elsereturn false;} @ overridepublic e next () {return (e) oldo [currentindex ++] ;}}
Node
Package COM. rain. iterator; public class node {private object data; private node next; Public node (Object Data, node next) {This. data = data; this. next = next;} public object getdata () {return data;} public void setdata (Object Data) {This. data = data;} public node getnext () {return next;} public void setnext (node next) {This. next = next;} @ overridepublic string tostring () {return this. getdata (). tostring ();} p Ublic static void main (string [] ARGs) {node = new node ("mynode", null); node A = node; Node B = node; node c = new node ("CCCC", null); B. setnext (c); // If B has next, check whether a has next. Because AB points to the same memory nodesystem. out. println ("A data:" +. getdata (); system. out. println ("B data:" + B. getdata (); system. out. println ("A next data:" +. getnext (); system. out. println ("B next data:" + B. getnext ());}}
Mylinklist
Package COM. rain. iterator; public class mylinklist implements imycollection {int Index = 0; // subscript node head = NULL; // header node tail = NULL; // End Node @ overridepublic int size () {return this. index ;}@ overridepublic void add (Object E) {node = new node (E, null); If (this. head = NULL) {head = node; tail = head; this. index ++;} else {tail. setnext (node); tail = node; this. index ++ }}@ overridepublic imyiterator iterator () {// todo auto-generated method stubreturn new myiterator ();} class myiterator implements imyiterator {int currentindex = 0; @ overridepublic Boolean hasnext () {If (this. currentindex <index) return true; elsereturn false;} @ overridepublic object next () {If (this. currentindex = 0) {This. currentindex ++; return head. getdata ();} else {node next = head. getnext (); For (INT I = 1; I <this. currentindex; I ++) {next = next. getnext ();} This. currentindex ++; return next. getdata ();}}}}
Rainiterator main program
Package COM. rain. iterator; Class People {public int ID; Public people (int id) {This. id = ID ;}@ overridepublic string tostring () {return string. valueof (ID) ;}} public classrainiterator {public static void main (string [] ARGs) {// imycollection <people> collection = new myarraylist <people> (); imycollection <people> collection = new mylinklist (); For (INT I = 0; I <15; I ++) {collection. add (New People (I);} system. out. println (collection. size () + "element"); imyiterator <people> iterator = collection. iterator (); While (iterator. hasnext () {system. out. print (iterator. next () + "");}}}