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];// 容器 int index = 0;// 下标 @Override public int size() { return this.index; } @Override public void add(E e) { if (index >= oldO.length) {// 如果容器装满了 Object[] newO = new Object[oldO.length * 2];// 创建 新容器扩容 System.arraycopy(oldO, 0, newO, 0, oldO.length);// 将就容器的数据复制到新容器中 oldO = newO;// 将引用指向新容器 } oldO[index] = e; index++; } @Override public IMyIterator<E> iterator() { return new MyIterator(); } // 内部类 class MyIterator implements IMyIterator<E> { int currentIndex = 0; @Override public boolean hasNext() { if (this.currentIndex < index) return true; else return false; } @Override public 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; } @Override public String toString() { return this.getData().toString(); } public static void main(String[] args) { Node node=new Node("myNode", null); Node a=node; Node b=node; Node c=new Node("CCCC", null); b.setNext(c);//B有next了,现在看A会不会有next。因为AB指向的是同一块内存node System.out.println("A data:"+a.getData()); System.out.println("B data:"+b.getData()); System.out.println("A next data:"+a.getNext()); System.out.println("B next data:"+b.getNext()); } }
MyLinkList
package com.rain.Iterator; public class MyLinkList implements IMyCollection { int index=0;//下标 Node head=null;//头结点 Node tail=null;//尾结点 @Override public int size() { return this.index; } @Override public void add(Object e) { Node node=new Node(e, null); if(this.head==null){ head=node; tail=head; this.index++; } else{ tail.setNext(node); tail=node; this.index++; } } @Override public IMyIterator iterator() { // TODO Auto-generated method stub return new MyIterator(); } class MyIterator implements IMyIterator{ int currentIndex=0; @Override public boolean hasNext() { if(this.currentIndex<index) return true; else return false; } @Override public 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 主程序
package com.rain.Iterator; class People{ public int id; public People(int id) { this.id = id; } @Override public String toString() { return String.valueOf(id); } } public class RainIterator { 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()+"个元素"); IMyIterator<People> iterator=collection.iterator(); while(iterator.hasNext()){ System.out.print(iterator.next()+" "); } } }