• 关于iterator的一点疑惑


    书中代码:

    //: holding/SimpleIteration.java
    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]
    *///:~
    

    我主要困惑于hasNext()、next()函数的返回以及内部操作:

    while(it.hasNext()) {
        Pet p = it.next();
        System.out.print(p.id() + ":" + p + " ");
    }
    

    看书看到这里的时候有些困惑,然后在后面看到了一个实现了Iterator接口的内部类,读完这段代码之后,就理解了其运行原理:

    //: holding/NonCollectionSequence.java
    import typeinfo.pets.*;
    import java.util.*;
    
    class PetSequence {
      protected Pet[] pets = Pets.createArray(8);
    }
    
    public class NonCollectionSequence extends PetSequence {
      public Iterator<Pet> iterator() {
        return new Iterator<Pet>() {
          private int index = 0;
          public boolean hasNext() {
            return index < pets.length;
          }
          public Pet next() { return pets[index++]; }
          public void remove() { // Not implemented
            throw new UnsupportedOperationException();
          }
        };
      }
      public static void main(String[] args) {
        NonCollectionSequence nc = new NonCollectionSequence();
        InterfaceVsIterator.display(nc.iterator());
      }
    } /* Output:
    0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx
    *///:~
    

    观察这段代码:

    return new Iterator<Pet>() {
        private int index = 0;
        public boolean hasNext() {
        return index < pets.length;
        }
        public Pet next() { return pets[index++]; }
        public void remove() { // Not implemented
        throw new UnsupportedOperationException();
        }
    };
    

    由hasNext()函数的实现可以猜测List中的元素的索引是从0开始的(事实也确实如此),然后是next()函数,看到index++这块代码,我们就理解了,next()函数返回的当前索引位置的元素,然后还有一个将索引递增的操作。

  • 相关阅读:
    wingIDE Pro6 破解教程
    C++中的访问权限
    解决wine中文字体方块或乱码
    linux下目录的作用
    linux下查看系统信息
    Windows Eclipse Maven 安装
    Centos SVN 搭建
    Mysql MyISAM 与 InnoDB 效率
    Linux删除除指定后缀外的所有文件
    mysql 多个timestamp设置自动更新 错误:there can be only one TIMESTAMP column with CURRENT_TIMESTAMP
  • 原文地址:https://www.cnblogs.com/fanlumaster/p/13661742.html
Copyright © 2020-2023  润新知