设计模式之迭代器模式(代码)

来源:互联网 发布:网络记者证怎么办理 编辑:程序博客网 时间:2024/04/30 13:20

下面是模拟java里面的ArrayList和LinkedList的迭代器的实现原理

(1)首先创建一个集合类Collection

package com.alan.iterator;public interface Collection {//添加元素void add(Object o) ;//得到长度int size() ;//得到迭代器Iterator iterator() ;}

(2)创建迭代器接口Iterator

package com.alan.iterator;public interface Iterator {//判断下一个有没有元素boolean hasNext() ;//得到下一个元素Object next() ;}

(3)创建ArrayList类

package com.alan.iterator;import java.util.Arrays;public class ArrayList implements Collection{int DEFAULT_SIZE = 10 ;Object[] elementData ;int index ;public ArrayList(){elementData = new Object[DEFAULT_SIZE] ;}public ArrayList(int size){elementData = new Object[size] ;}@Overridepublic void add(Object o) {//检查数组是否已满if(index>=elementData.length){//扩容ensureCapacity() ;}elementData[index] = o ;index++ ;}//给数组扩容public void ensureCapacity(){Object[] newContainer = new Object[elementData.length*2] ;//扩容为原来的两倍System.arraycopy(elementData, 0, newContainer, 0, elementData.length) ;elementData = new Object[newContainer.length] ;elementData = newContainer ;}@Overridepublic Iterator iterator() {return new ArrayListIterator();}@Overridepublic int size() {return 0;}private class ArrayListIterator implements Iterator{int cousor =0 ;@Overridepublic boolean hasNext() {if(cousor <index){return true ;}return false;}@Overridepublic Object next() {Object o = elementData[cousor] ;cousor++ ;return o;}}}

(5)创建LinkedList类

package com.alan.iterator;public class LinkedList implements Collection{private Node header ;private Node next ;private int index ;public LinkedList(){header = new Node(null) ;}@Overridepublic void add(Object o) {// TODO Auto-generated method stubif(next ==null){next = new Node(o) ;header.next = next ;}else{Node node = new Node(o) ;next.next = node ;next = node ;}index++ ;}@Overridepublic Iterator iterator() {// TODO Auto-generated method stubreturn new LinkedListIterator();}@Overridepublic int size() {// TODO Auto-generated method stubreturn index;}private class LinkedListIterator implements Iterator{int cousor = 0 ;Node n ;@Overridepublic boolean hasNext() {if(cousor <index){return true ;}return false;}@Overridepublic Object next() {if(cousor == 0){n = header ;}n = n.next ;cousor++ ;return n.elementData;}}private class Node{Node next ;Object elementData ;public Node(Object elementData){this.elementData = elementData ;}}}

(6)创建测试类Client

package com.alan.iterator;public class Client {public static void main(String[] args) {//模拟LinkedListLinkedList list = new LinkedList() ;for(int i=0;i<10;i++){list.add(new Test(i)) ;}//模拟LinkedList的Iterator实现Iterator it = list.iterator() ;while(it.hasNext()){Object o = it.next() ;System.out.println("LinkedList:"+o);}//模拟ArrayListArrayList arrayList = new ArrayList() ;for(int i=0;i<20;i++){arrayList.add(new Test(i)) ;}//便利arrayListIterator arrayIt = arrayList.iterator() ;while(arrayIt.hasNext()){Object o = arrayIt.next() ;System.out.println("ArrayList:"+o);}}}class Test{int id ;public Test(int id){this.id = id ;}@Overridepublic String toString(){return "obj:"+id+"  " ;}}

输出的结果:

LinkedList:obj:0  LinkedList:obj:1  LinkedList:obj:2  LinkedList:obj:3  LinkedList:obj:4  LinkedList:obj:5  LinkedList:obj:6  LinkedList:obj:7  LinkedList:obj:8  LinkedList:obj:9  ArrayList:obj:0  ArrayList:obj:1  ArrayList:obj:2  ArrayList:obj:3  ArrayList:obj:4  ArrayList:obj:5  ArrayList:obj:6  ArrayList:obj:7  ArrayList:obj:8  ArrayList:obj:9  ArrayList:obj:10  ArrayList:obj:11  ArrayList:obj:12  ArrayList:obj:13  ArrayList:obj:14  ArrayList:obj:15  ArrayList:obj:16  ArrayList:obj:17  ArrayList:obj:18  ArrayList:obj:19