zoukankan      html  css  js  c++  java
  • 第三节:迭代器模式——JDK中集合应用的源码分析

    迭代器模式在 JDK-ArrayList 集合应用的源码分析

      1、JDK 的 ArrayList 集合中就使用了迭代器模式;

      2、代码分析:

     1 public class IteratorTest {
     2 
     3     public static void main(String[] args) {
     4         List<String> list = new ArrayList<>();
     5         list.add("Tom");
     6 
     7         Iterator<String> iterator = list.iterator();
     8         while (iterator.hasNext()) {
     9             System.out.println(iterator.next());
    10         }
    11 
    12         LinkedList<String> linkedList = new LinkedList<>();
    13     }
    14 }

      说明:

      3、源码

       ArrayList:

     

      4、类图

              

      对类图的角色分析和说明:

      (1)内部类 Itr 充当具体实现迭代器 Iterator 的类,作为 ArrayList 内部类;

      (2)List 就充当了聚合接口,含有一个 Iterator() 方法,返回一个迭代器对象;

      (3)ArrayList 是实现聚合接口 List 的子类,实现了 iterator();

      (4)Iterator 接口系统提供;

      (5)迭代器模式解决了不同集合 (ArrayList、LinkedList)统一遍历问题;

  • 相关阅读:
    python day01
    Mac上安装pexpect
    raid
    SSL证书制作
    linux grep命令详解
    第一轮迭代小组成员分数分配
    M1事后分析报告(Postmortem Report)
    软件发布说明
    测试报告
    week 9 scenario testing
  • 原文地址:https://www.cnblogs.com/niujifei/p/14400220.html
Copyright © 2011-2022 走看看