首页 > 代码库 > 迭代器模式

迭代器模式

ref: http://blog.csdn.net/fengzhe0411/article/details/7187905

迭代器模式我们在jdbc,和容器中经常用到。每个容器要想遍历的时候,我们会调用

xxx.iterator();返回迭代器。

首先我们先看看迭代器接口

1 Method Summary2  boolean    hasNext() 3           Returns true if the iteration has more elements.4  E    next() 5           Returns the next element in the iteration.6  void    remove() 7           Removes from the underlying collection the last element returned by the iterator (optio

3个方法,hashnext 还有下一个元素吗?  next:获得下一个元素的对象
迭代器屏蔽了各种实现细节,对于hashmap,LinkedList都可以用迭代器访问,而不用了解内部实现的细节。

下面使用Iterator接口,和Collection接口实现一个LinkedList集合。

  1 import java.util.Collection;  2 import java.util.Iterator;  3   4 //链表迭代器的实现  5 class Node  6 {  7     Object n;  8     Node next;  9     public Node(Object n) 10     { 11         this.n=n; 12          13     } 14      15 } 16 public class LinkList  implements Collection{ 17     Node head=null; 18     Node tail=null; 19     int size=0; 20     21      22     /** 23      * @param args 24      */ 25     public static void main(String[] args) { 26         // TODO Auto-generated method stub 27         LinkList l=new LinkList(); 28         l.add(new Node("zhang")); 29         l.add(new Node("xiao")); 30         l.add(new Node("er")); 31         l.add(new Node("shi")); 32         Iterator iter=l.iterator(); 33         while(iter.hasNext()) 34         { 35             System.out.println(iter.next()); 36              37         } 38  39     } 40  41     @Override 42     public int size() { 43         // TODO Auto-generated method stub 44         return size; 45     } 46  47     @Override 48     public boolean isEmpty() { 49         if(head==null) return true; 50         return false; 51     } 52  53     @Override 54     public boolean contains(Object o) { 55         // TODO Auto-generated method stub 56         Node p=head; 57         while(p!=null) 58         { 59             if(p.n==o) return true; 60             p=p.next; 61         } 62         return false; 63     } 64  65     @Override 66     public Iterator iterator() { 67         // TODO Auto-generated method stub 68         return new LinkListIterator(); 69     } 70  71     @Override 72     public Object[] toArray() { 73         // TODO Auto-generated method stub 74         return null; 75     } 76  77     @Override 78     public Object[] toArray(Object[] a) { 79         // TODO Auto-generated method stub 80         return null; 81     } 82  83     @Override 84     public boolean add(Object e) { 85         // TODO Auto-generated method stub 86         if(head==null) 87         { 88             head=(Node) e; 89             tail=head; 90         } 91         else 92         { 93             tail.next=(Node) e; 94             tail=tail.next; 95              96         } 97         return true; 98     } 99 100     @Override101     public boolean remove(Object o) {102         // TODO Auto-generated method stub103         return false;104     }105 106     @Override107     public boolean containsAll(Collection c) {108         // TODO Auto-generated method stub109         return false;110     }111 112     @Override113     public boolean addAll(Collection c) {114         // TODO Auto-generated method stub115         return false;116     }117 118     @Override119     public boolean removeAll(Collection c) {120         // TODO Auto-generated method stub121         return false;122     }123 124     @Override125     public boolean retainAll(Collection c) {126         // TODO Auto-generated method stub127         return false;128     }129 130     @Override131     public void clear() {132         // TODO Auto-generated method stub133         134     }135     private  class LinkListIterator implements Iterator136     {137         private Node current=head;138 139         @Override140         public boolean hasNext() {141             // TODO Auto-generated method stub142             if(current==null) return false;143             return true;144         }145 146         @Override147         public Object next() {148             Object o=current.n;149               current=current.next;150             return o;151         }152 153         @Override154         public void remove() {155             // TODO Auto-generated method stub156             157         }158         159         160     }161 162 }