2021-11-14Iterator迭代器

1.Iterator迭代器介绍

集合的遍历有两种:迭代器或者增强for循环。为什么不用for循环获得每个元素呢?因为在list里面有索引,但是在set里面没有索引。
在这里插入图片描述有索引的时候可以用for循环,但是集合中有的没有索引,所以用Iterator迭代器进行遍历
在这里插入图片描述

2.迭代器的代码实现

Demo01Iterator.java

package Iterator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/*
 *   java.util.Iterator接口:迭代器(对集合进行遍历)
 *   Iterator迭代器,是一个接口,我们无法直接使用,需要使用Iterator接口的实现对象,获取实现类的方式比较特殊
 *   Collection接口中又一个方法,叫iterator(),这个方法返回的就是迭代器的实现类对象
 *       Iterator<E> iterator() 返回在此collection的元素上进行迭代的迭代器
 *
 *   迭代器的使用步骤(重点):
 *       1.使用集合中的方法iterator()获取迭代器的实现类对象,使用Iterator接口接收(多态)
 *       2.使用Iterator接口中的方法hasNext判断还有没有下一个元素
 *       3.使用Iterator接口中的方法next去除集合中的下一个元素
 * */
public class Demo01Iterator {
    public static void main(String[] args) {
        //1.创建集合对象
        Collection<String> coll = new ArrayList<>();

        //2、往集合里面添加元素
        coll.add("张三");
        coll.add("李四");
        coll.add("王五");
        coll.add("赵六");
        coll.add("田七");

        /*
         * 1.使用集合中的方法iterator()获取迭代器的实现类对象,使用Iterator接口接收(多态)
         * 注意:
         *       Iterator<E>接口也是有泛型的,迭代器的泛型跟着集合走,集合是什么泛型,迭代器就是什么泛型
         * */
        //多态
        Iterator<String> it = coll.iterator();

        /*
         *  发现使用迭代器取出集合中元素的代码,是一个重复的过程
         *  所以我们可以使用循环优化
         *  不知道集合中有多少个元素,使用while循环
         *  循环结束的条件,hasNext方法返回false
         *
         * */

        while (it.hasNext()) {
            System.out.println(it.next());
        }
        //张三
        //李四
        //王五
        //赵六
        //田七

//        boolean b = it.hasNext();
//        System.out.println(b);//true
//        String next = it.next();
//        System.out.println(next);//张三
//
//        boolean b1 = it.hasNext();
//        System.out.println(b1);//true
//        String next1 = it.next();
//        System.out.println(next1);//李四
//
//        boolean b2 = it.hasNext();
//        System.out.println(b2);//true
//        String next2 = it.next();
//        System.out.println(next2);//王五
//
//        boolean b3 = it.hasNext();
//        System.out.println(b3);//true
//        String next3 = it.next();
//        System.out.println(next3);//赵六
//
//        boolean b4 = it.hasNext();
//        System.out.println(b4);//true
//        String next4 = it.next();
//        System.out.println(next4);//田七
//
//        boolean b5 = it.hasNext();
//        System.out.println(b5);//false
//        String next5 = it.next();
//        //System.out.println(next5);//NoSuchElementException

    }
}

3.迭代器的原理

在这里插入图片描述

4.增强For

在这里插入图片描述Demo02ForEach.java

package Iterator;

import java.util.ArrayList;
import java.util.Collection;

public class Demo02ForEach {
    public static void main(String[] args) {
        //1.遍历数组
        int[] array = new int[]{1, 2, 3, 4, 5};
        for (int a : array) {
            System.out.println(a);
        }
        //1
        //2
        //3
        //4
        //5

        //2.遍历集合
        Collection<String> coll = new ArrayList<>();
        coll.add("张三");
        coll.add("李四");
        coll.add("王五");
        coll.add("赵六");
        coll.add("田七");

        for (String c : coll) {
            System.out.println(c);
        }

        //张三
        //李四
        //王五
        //赵六
        //田七
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值