zoukankan      html  css  js  c++  java
  • java iterator(迭代器)

         任何容器类,都必须有某种方式可以插入元素并将它们再次取出,毕竟持有事物是容器最基本的工作,对于List,add()插入fang,get()取出,如果从更高层的角度思考,会发现这里有个确定:要用容器,必须对容器的确切类型编程,这样如果原本是List编码的,但是后来要应用于Set,那么此时该怎么办,是重写通用代码还是如何

         迭代器(也是一种设计模式)的概念可用于达成这个目的,迭代器是一个对象,它的工作是遍历并选择序列中 对象,而客服端程序员不必关心或知道该序列的底层结构,此外迭代器通常被称为轻量级对象:创建它的代价很小. 因此经常可以见到对迭代器有些奇怪的限制;例如,Java的Iterator只能单向移动,这个Iterator只能用来:

    1)使用方法iterator()要求容器返回一个Iterator. Iterator将准备好返回序列的第一个元素

    2)使用next()获得序列中的下一个元素

    3)使用hasNext()检查序列中是否还有元素

    4)使用remove()将迭代器新近返回的元素删除 (必须先调用next())

    package java.util;
    
    import java.util.function.Consumer;
    public interface Iterator<E> {
        boolean hasNext();   //检查是否有下一个元素
        E next();            //返回下一个元素
        default void remove() { //移除一个elment
            throw new UnsupportedOperationException("remove");
        }
        default void forEachRemaining(Consumer<? super E> action) {
            Objects.requireNonNull(action);
            while (hasNext())
                action.accept(next());
        }
    }
    //: holding/SimpleIteration.java
    package object;
    
    import typeinfo.pets.*;
    import java.util.*;
    
    public class SimpleIteration {
      public static void main(String[] args) {
        List<Pet> pets = Pets.arrayList(12);
         Iterator<Pet> it = pets.iterator();
        while(it.hasNext()) {
          Pet p = it.next();
          System.out.print(p.id() + ":" + p + " ");
        }
        System.out.println();
        // A simpler approach, when possible:
        for(Pet p : pets)
          System.out.print(p.id() + ":" + p + " ");
        System.out.println();    
        // An Iterator can also remove elements:
        it = pets.iterator();
        for(int i = 0; i < 6; i++) {
          it.next();
          it.remove();
        }
        System.out.println(pets);
      }
    } /* Output:
    0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx 8:Cymric 9:Rat 10:EgyptianMau 11:Hamster
    0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx 8:Cymric 9:Rat 10:EgyptianMau 11:Hamster
    [Pug, Manx, Cymric, Rat, EgyptianMau, Hamster]
    *///:~

      接受对象容器并传递它,从而在每个对象上都执行操作,这种思想十分强大,现在创建一个display()方法,它不必知道容器的确切类型,清注意diplay()不包含任何有关它所遍历的序列的类型信息,而这也展示了Iterator的真正威力: 能够将遍历序列的操作与序列底层的结构分离,正由于此,我们有时会说,迭代器统一了对容器的访问方式

        //: holding/CrossContainerIteration.java
        package object;
        import typeinfo.pets.*;
        import java.util.*;
        
        public class CrossContainerIteration {
          public static void display(Iterator<Pet> it) {
            while(it.hasNext()) {
              Pet p = it.next();
              System.out.print(p.id() + ":" + p + " ");
            }
            System.out.println();
          }    
          public static void main(String[] args) {
            ArrayList<Pet> pets = Pets.arrayList(8);
            LinkedList<Pet> petsLL = new LinkedList<Pet>(pets);
            HashSet<Pet> petsHS = new HashSet<Pet>(pets);
            TreeSet<Pet> petsTS = new TreeSet<Pet>(pets);
            display(pets.iterator());
            display(petsLL.iterator());
            display(petsHS.iterator());
            display(petsTS.iterator());
          }
        } /* Output:
        0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx
        0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx
        4:Pug 6:Pug 3:Mutt 1:Manx 5:Cymric 7:Manx 2:Cymric 0:Rat
        5:Cymric 2:Cymric 7:Manx 1:Manx 3:Mutt 6:Pug 4:Pug 0:Rat
        *///:~
  • 相关阅读:
    android手机开发网
    Android 使用Google Weather制作天气预报程序
    android反编译xml文件
    在线MSDN
    工作的思考一:业务流和工作管理
    .NET Remoting技术文章汇总
    学习之路十四:客户端调用WCF服务的几种方法小议
    工作的思考二:无效的沟通
    ADO.NET 全面梳理
    工作的思考三:工作质量,计划制定,沟通交流
  • 原文地址:https://www.cnblogs.com/jiangfeilong/p/10252863.html
Copyright © 2011-2022 走看看