A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 潘多拉 中级黑马   /  2014-9-4 16:51  /  2268 人查看  /  11 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

迭代器为啥不是new出来的呢?

11 个回复

倒序浏览
这是因为创建对象的逻辑被iterator()方法封装了
回复 使用道具 举报
因为是创建集合后,调用iterator方法后返回的Iterator类型的对象才具备hasNext方法、next方法等,而这些方法所在的类是被封装在集合内部的,所以去除方式就可以直接访问集合内部元素。
回复 使用道具 举报
额,因为那些集合实现了Iterator接口,由于数据结构不同,遍历的方式也不同,所以需要集合自定义变量方法,但是为了统一所以实现相同接口,以便于规范。
回复 使用道具 举报
好高深的回答
回复 使用道具 举报
我的理解是:其实它就是对象,只不过是被封装在iterator()方法中
回复 使用道具 举报
被封装了!
回复 使用道具 举报
具有迭代功能的集合是因为实现了Iterable接口, Iterable接口中只定义的一个返回值为Interator类型的iterator()方法,   实现Iterable接口的集合中覆写了iterator(),返回一个符合该集合数据结构的Iterator对象。
回复 使用道具 举报
我也没看懂!!
回复 使用道具 举报
以ArrayList为例,看一下它的源代码:
  1. package java.util;

  2. public class ArrayList<E> extends AbstractList<E>
  3.                                       implements List<E>, RandomAccess, Cloneable, java.io.Serializable
  4. {

  5.         //这是从Collection接口继承得到的iterator方法
  6.         public Iterator<E> iterator() {
  7.                //下面的代码返回了一个Itr类的实例
  8.         return new Itr();
  9.     }

  10.     //私有内部类Itr,实现了Iterable接口
  11.     private class Itr implements Iterator<E> {
  12.         int cursor;       // index of next element to return
  13.         int lastRet = -1; // index of last element returned; -1 if no such
  14.         int expectedModCount = modCount;

  15.         public boolean hasNext() {
  16.             return cursor != size;
  17.         }

  18.         @SuppressWarnings("unchecked")
  19.         public E next() {
  20.             checkForComodification();
  21.             int i = cursor;
  22.             if (i >= size)
  23.                 throw new NoSuchElementException();
  24.             Object[] elementData = ArrayList.this.elementData;
  25.             if (i >= elementData.length)
  26.                 throw new ConcurrentModificationException();
  27.             cursor = i + 1;
  28.             return (E) elementData[lastRet = i];
  29.         }

  30.         public void remove() {
  31.             if (lastRet < 0)
  32.                 throw new IllegalStateException();
  33.             checkForComodification();

  34.             try {
  35.                 ArrayList.this.remove(lastRet);
  36.                 cursor = lastRet;
  37.                 lastRet = -1;
  38.                 expectedModCount = modCount;
  39.             } catch (IndexOutOfBoundsException ex) {
  40.                 throw new ConcurrentModificationException();
  41.             }
  42.         }

  43.         final void checkForComodification() {
  44.             if (modCount != expectedModCount)
  45.                 throw new ConcurrentModificationException();
  46.         }
  47.     }
  48. }
复制代码

从上面代码可以看出,ArrayList的iterator方法返回的是一个内部类Itr的实例,该内部类实现了Iterator接口。

不仅仅是ArrayList,你可以从其他的集合类中找到类似的代码。


回复 使用道具 举报
iterator的构造函数被私有化了,但调用iterator()函数可以返回一个Iterator类型的对象。这是单例设计的思想。
回复 使用道具 举报
迭代器接口出现:将每一个容器中的取出方式进行了封装。并对外暴露。
这样无论是什么容器或者数据结构,只要内部取出方式实现了Iterator接口
都可以通过该接口取出这些容器中的元素
它的出现,将容器的取出方式和容器的数据结构相分离,降低了耦合性
而取出方式因为直接在访问容器中的元素,并依赖具体的数据结构,所以被定义在了容器中
通过内部类来实现Itertor接口
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马