在java的集合中,List接口继承Collection接口,AbstractList类实现了List接口,在AbstractList中的内部类Itr实现了Iterator接口

ArrayList实现List接口并继承AbstractList类,结构图如下:(图片出自网络)

Iterator接口源码:

  1. public interface Iterator<E> {
  2. boolean hasNext();
  3. E next();
  4.  
  5. default void remove() {
  6. throw new UnsupportedOperationException("remove");
  7. }
  8.  
  9. default void forEachRemaining(Consumer<? super E> action) {
  10. Objects.requireNonNull(action);
  11. while (hasNext())
  12. action.accept(next());
  13. }
  14. }

AbstractList的内部类Itr实现了Iterator接口,如下所示:

  1. private class Itr implements Iterator<E> {
  2. /**元素的下标
  3. * Index of element to be returned by subsequent call to next.
  4. */
  5. int cursor = 0;
  6.  
  7. /**上一个元素的下标。如果元素已被删除就设置为-1
  8. * Index of element returned by most recent call to next or
  9. * previous. Reset to -1 if this element is deleted by a call
  10. * to remove.
  11. */
  12. int lastRet = -1;
  13.  
  14. /**允许修改的次数,违规操作会抛异常
  15. * The modCount value that the iterator believes that the backing
  16. * List should have. If this expectation is violated, the iterator
  17. * has detected concurrent modification.
  18. */
  19. int expectedModCount = modCount;
  20. /*检查是否还有下一个元素*/
  21. public boolean hasNext() {
  22. return cursor != size();
  23. }
  24. /*光标下移,并且返回当前的元素*/
  25. public E next() {
  26. checkForComodification();
  27. try {
  28. int i = cursor;
  29. E next = get(i);
  30. lastRet = i;
  31. cursor = i + 1;
  32. return next;
  33. } catch (IndexOutOfBoundsException e) {
  34. checkForComodification();
  35. throw new NoSuchElementException();
  36. }
  37. }
  38. /*移除元素*/
  39. public void remove() {
  40. if (lastRet < 0)
  41. throw new IllegalStateException();
  42. checkForComodification();
  43.  
  44. try {
  45. AbstractList.this.remove(lastRet);
  46. if (lastRet < cursor)
  47. cursor--;
  48. lastRet = -1;
  49. expectedModCount = modCount;
  50. } catch (IndexOutOfBoundsException e) {
  51. throw new ConcurrentModificationException();
  52. }
  53. }
  54.  
  55. final void checkForComodification() {
  56. if (modCount != expectedModCount)
  57. throw new ConcurrentModificationException();
  58. }
  59. }

ArrayList中的iterator()方法:

  1. public Iterator<E> iterator() {
  2. return new Itr();
  3. }

ArrayList中的内部类Itr源码功能类似于AbstractList的内部类Itr。

阅读了Iterator的源码,再回头看Iterator遍历List的过程,理解就会深刻很多。

  1. List<String> list=new ArrayList<String>();
  2. list.add("apple"); list.add("banana"); list.add("watermelon");
  3. for (Iterator<String> iterator=list.iterator();iterator.hasNext();) {
  4. System.out.println( iterator.next());
    }

java的Iterator源码浅析的更多相关文章

  1. java.lang.Integer源码浅析

    Integer定义,final不可修改的类 public final class Integer extends Number implements Comparable<Integer> ...

  2. Java基础—ArrayList源码浅析

    注:以下源码均为JDK8的源码 一. 核心属性 基本属性如下: 核心的属性其实是红框中的两个: //从注释也容易看出,一个是集合元素,一个是集合长度(注意是逻辑长度,即元素的个数,而非数组长度) 其中 ...

  3. 我对java String的理解 及 源码浅析

    摘要: 摘要: 原创出处: http://www.cnblogs.com/Alandre/ 泥沙砖瓦浆木匠 希望转载,保留摘要,谢谢! 每天起床告诉自己,自己的目标是 ”技术 + 英语 还有生活“! ...

  4. java并发:jdk1.8中ConcurrentHashMap源码浅析

    ConcurrentHashMap是线程安全的.可以在多线程中对ConcurrentHashMap进行操作. 在jdk1.7中,使用的是锁分段技术Segment.数据结构是数组+链表. 对比jdk1. ...

  5. HashSet其实就那么一回事儿之源码浅析

    上篇文章<HashMap其实就那么一回事儿之源码浅析>介绍了hashMap,  本次将带大家看看HashSet, HashSet其实就是基于HashMap实现, 因此,熟悉了HashMap ...

  6. Android 手势识别类 ( 三 ) GestureDetector 源码浅析

    前言:上 篇介绍了提供手势绘制的视图平台GestureOverlayView,但是在视图平台上绘制出的手势,是需要存储以及在必要的利用时加载取出手势.所 以,用户绘制出的一个完整的手势是需要一定的代码 ...

  7. Android开发之Theme、Style探索及源码浅析

    1 背景 前段时间群里有伙伴问到了关于Android开发中Theme与Style的问题,当然,这类东西在网上随便一搜一大把模板,所以关于怎么用的问题我想这里也就不做太多的说明了,我们这里把重点放在理解 ...

  8. Java文件操作源码大全

    Java文件操作源码大全 1.创建文件夹 52.创建文件 53.删除文件 54.删除文件夹 65.删除一个文件下夹所有的文件夹 76.清空文件夹 87.读取文件 88.写入文件 99.写入随机文件 9 ...

  9. (二)一起学 Java Collections Framework 源码之 AbstractCollection

    . . . . . 目录 (一)一起学 Java Collections Framework 源码之 概述(未完成) (二)一起学 Java Collections Framework 源码之 Abs ...

随机推荐

  1. Webpack 入门指南 - 3. Hello, Angular2!

    Webpack 入门指南 - 1.安装 Webpack 入门指南 - 2.模块 这一次,我们使用 Webpack 来打包 Angular 2 的应用. 与官方的 Hello, Angular 2 项目 ...

  2. <select> 标签使用

    jQuery获取Select选择的Text和Value: 1. var checkText=jQuery("#select_id").find("option:selec ...

  3. python download

    今天下载 python3 , 从官网下 速度 平均 十几K,网上 搜了下.提供的下载地址 几乎都是 官网的. 于是 下了个 百度同步盘,做 公开分享. 提供给 大家下载,速度 有 300 多K,提高了 ...

  4. 一致性 hash 算法( consistent hashing )a

    一致性 hash 算法( consistent hashing ) 张亮 consistent hashing 算法早在 1997 年就在论文 Consistent hashing and rando ...

  5. [转]解决a different object with the same identifier value was already associated with the session错误

    1.a different object with the same identifier value was already associated with the session. 错误原因:在h ...

  6. Objective C ARC 使用及原理

    手把手教你ARC ,里面介绍了ARC的一些特性, 还有将非ARC工程转换成ARC工程的方法 ARC 苹果官方文档 下面用我自己的话介绍一下ARC,并将看文档过程中的疑问和答案写下来.下面有些是翻译,但 ...

  7. python转exe的小工具

    其实只是在cxfreeze的基础上加了个壳,做成窗口软件了 使用了pyqt做的界面,软件发布在了开源中国上,可以直接去下面的地址查看 http://git.oschina.net/robocky/py ...

  8. OC Runtime

    OC 是面向运行时的语言.Runtime就是系统在运行的时候的一些机制,其中最主要的是消息发送机制.OC语言与其他语言(如C语言)在函数(方法)的调用有很大的不同.C语言,函数的调用在编译的时候就已经 ...

  9. NHibernate系列文章二十二:NHibernate查询之HQL查询(附程序下载)

    摘要 NHibernate提供了多种查询方式,最早的HQL语言查询.Criteria查询和SQL Query,到NHibernate 3.0的Linq NHibernate,NHIbernate 4. ...

  10. Leetcode4:Median of Two Sorted Arrays@Python

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...