迭代器简述

来源:互联网 发布:ubuntu 14.04 u盘制作 编辑:程序博客网 时间:2024/06/04 17:25
转自:http://289196801.iteye.com/blog/1511867
在jdk中,与迭代器相关的接口有两个:Iterator 与 Iterable 

Iterator:迭代器,Iterator及其子类通常是迭代器本身的结构与方法; 
Iterable:可迭代的,那些想用到迭代器功能的其它类,如AbstractList HashMap等,需要实现该接口。 

以下为两个接口源码: 
Iterator 如下: 
Java代码  收藏代码
  1.    
  2. 1.public interface Iterator {    
  3. 2.    boolean hasNext();    
  4. 3.    E next();    
  5. 4.    void remove();    
  6. 5.}    

Iterable 如下: 
Java代码  收藏代码
  1. 1.public interface Iterable {    
  2. 2.    Iterator iterator();    
  3. 3.}    


看完源码,我们来看看迭代器是如何使用的: 
1. 若类A想要使用迭代器,则它的类声明部分为 class A implement Iterable 
2. 在类A实现中,要实现Iterable接口中的唯一方法:Iterator iterator(); 
   这个方法用于返回一个迭代器,即Iterator接口及其子类; 
3. 在类A中,定义一个内部类S,专门用于实现Iterator接口,定制类A自已的迭代器实现。 
如下: 
Java代码  收藏代码
  1. 1.class A implement Iterable    
  2. 2.{    
  3. 3.    Iterator iterator() {...}    
  4. 4.    class S implement Iterator     
  5. 5.    {    
  6. 6.        boolean hasNext() {....}    
  7. 7.        E next() {....}    
  8. 8.        void remove() {....}    
  9. 9.    }    
  10. 10.}    


我们再来看看jdk中是如何来使用的,如抽象类AbstractList: 
Java代码  收藏代码
  1. 1.public abstract class AbstractList extends AbstractCollection implements List { // List接口实现了Collection, Iterable   
  2. 2.    
  3. 3.    protected AbstractList() {    
  4. 4.    }    
  5. 5.      
  6. 6.    ...    
  7. 7.    
  8. 8.    public Iterator iterator() {    
  9. 9.    return new Itr();  // 这里返回一个迭代器  
  10. 10.    }    
  11. 11.    
  12. 12.    private class Itr implements Iterator {  // 内部类Itr实现迭代器  
  13. 13.         
  14. 14.    int cursor = 0;    
  15. 15.    int lastRet = -1;    
  16. 16.    int expectedModCount = modCount;    
  17. 17.    
  18. 18.    public boolean hasNext() {  // 实现hasNext方法  
  19. 19.            return cursor != size();    
  20. 20.    }    
  21. 21.    
  22. 22.    public E next() {  // 实现next方法  
  23. 23.            checkForComodification();    
  24. 24.        try {    
  25. 25.        E next = get(cursor);    
  26. 26.        lastRet = cursor++;    
  27. 27.        return next;    
  28. 28.        } catch (IndexOutOfBoundsException e) {    
  29. 29.        checkForComodification();    
  30. 30.        throw new NoSuchElementException();    
  31. 31.        }    
  32. 32.    }    
  33. 33.    
  34. 34.    public void remove() {  // 实现remove方法  
  35. 35.        if (lastRet == -1)    
  36. 36.        throw new IllegalStateException();    
  37. 37.            checkForComodification();    
  38. 38.    
  39. 39.        try {    
  40. 40.        AbstractList.this.remove(lastRet);    
  41. 41.        if (lastRet < cursor)    
  42. 42.            cursor--;    
  43. 43.        lastRet = -1;    
  44. 44.        expectedModCount = modCount;    
  45. 45.        } catch (IndexOutOfBoundsException e) {    
  46. 46.        throw new ConcurrentModificationException();    
  47. 47.        }    
  48. 48.    }    
  49. 49.    
  50. 50.    final void checkForComodification() {    
  51. 51.        if (modCount != expectedModCount)    
  52. 52.        throw new ConcurrentModificationException();    
  53. 53.    }    
  54. 54.    }    
  55. 55.}    



看完了迭代器,我们再看看迭代器模式: 
1. 提出问题:什么是迭代器模式? 为什么会有迭代器模式? 
   GOF给出的定义为:提供一种方法访问一个容器(container)对象中各个元素,而又不 
                     需暴露该对象的内部细节。 
    我的理解: 就是把遍历算法从容器对象中独立出来,为什么要把遍历算从容器对象中独立 
              出来呢? 因为在面向对象设计中,一个难点就是辨认对象的职责。理想的状 
              态下,一个类应该只有一个单一的职责。职责分离可以最大限度的去耦合,但 
              是职责单一说起来容易,做起来难。具体到本模式,我们明显可以看到,一个 
              容器对象它提供了两个职责:一是组织管理数据对象,二是提供遍历算法。 
    所以:Iterator模式就是分离了集合对象的遍历行为,抽象出一个迭代器类来负责,这样 
         既可以做到不暴露集合的内部结构,又可让外部代码透明的访问集合内部的数据。 

2. 下面开始讲迭代器模式: 
   迭代器模式由以下角色组成: 
  1) 迭代器角色(Iterator):迭代器角色负责定义访问和遍历元素的接口。 
  2) 具体迭代器角色(Concrete Iterator):具体迭代器角色要实现迭代器接口,并 
        要记录遍历中的当前位置。 
    3) 容器角色(Container):容器角色负责提供创建具体迭代器角色的接口。 
  4) 具体容器角色(Concrete Container):具体容器角色实现创建具体迭代器角色 
        的接口——这个具体迭代器角色于该容器的结构相关。 

    迭代器模式的类图如下: 
     
   
转自:http://289196801.iteye.com/blog/1511867
0 0
原创粉丝点击