• Iterator迭代器接口(遍历Collection的两种方式之一)


    使用 Iterator 接口遍历集合元素:
     Iterator对象称为迭代器(设计模式的一种),主要用于遍历 Collection 集合中的元素。
     GOF给迭代器模式的定义为:提供一种方法访问一个容器(container)对象中各个元
    素,而又不需暴露该对象的内部细节。 迭代器模式,就是为容器而生。类似于“公
    交车上的售票员”、“火车上的乘务员”、“空姐”。
     Collection接口继承了java.lang.Iterable接口,该接口有一个iterator()方法,那么所
    有实现了Collection接口的集合类都有一个iterator()方法,用以返回一个实现了
    Iterator接口的对象。
     Iterator 仅用于遍历集合,Iterator 本身并不提供承装对象的能力。如果需要创建
    Iterator 对象,则必须有一个被迭代的集合。
     集合对象每次调用iterator()方法都得到一个全新的迭代器对象,默认游标都在集合
    的第一个元素之前。

     

    * 集合元素的遍历操作,使用迭代器Iterator接口
    * 1.内部的方法:hasNext() 和 next()
    * 2.集合对象每次调用iterator()方法都得到一个全新的迭代器对象,
    * 默认游标都在集合的第一个元素之前。
    * 3.内部定义了remove(),可以在遍历的时候,删除集合中的元素。此方法不同于集合直接调用remove()

     

    @Test
        public void test1(){
            Collection coll = new ArrayList();
            coll.add(123);
            coll.add(456);
            coll.add(new Person("Jerry",20));
            coll.add(new String("Tom"));
            coll.add(false);
    
            Iterator iterator = coll.iterator();
            //方式一:
    //        System.out.println(iterator.next());
    //        System.out.println(iterator.next());
    //        System.out.println(iterator.next());
    //        System.out.println(iterator.next());
    //        System.out.println(iterator.next());
    //        //报异常:NoSuchElementException
    //        System.out.println(iterator.next());
    
            //方式二:不推荐
    //        for(int i = 0;i < coll.size();i++){
    //            System.out.println(iterator.next());
    //        }
    
            //方式三:推荐
            ////hasNext():判断是否还有下一个元素
            while(iterator.hasNext()){
                //next():①指针下移 ②将下移以后集合位置上的元素返回
                System.out.println(iterator.next());
            }
    
        }

    @Test
        public void test2(){
    
            Collection coll = new ArrayList();
            coll.add(123);
            coll.add(456);
            coll.add(new Person("Jerry",20));
            coll.add(new String("Tom"));
            coll.add(false);
    
            //错误方式一:
    //        Iterator iterator = coll.iterator();
    //        while((iterator.next()) != null){
    //            System.out.println(iterator.next());
    //        }
    
            //错误方式二:
            //集合对象每次调用iterator()方法都得到一个全新的迭代器对象,默认游标都在集合的第一个元素之前。
            while (coll.iterator().hasNext()){
                System.out.println(coll.iterator().next());
            }
    
    
        }
    
        //测试Iterator中的remove()
        //如果还未调用next()或在上一次调用 next 方法之后已经调用了 remove 方法,
        // 再调用remove都会报IllegalStateException。
    @Test
        public void test3(){
            Collection coll = new ArrayList();
            coll.add(123);
            coll.add(456);
            coll.add(new Person("Jerry",20));
            coll.add(new String("Tom"));
            coll.add(false);
    
            //删除集合中"Tom"
            Iterator iterator = coll.iterator();
            while (iterator.hasNext()){
    //            iterator.remove();
                Object obj = iterator.next();
                if("Tom".equals(obj)){
                    iterator.remove();
    //                iterator.remove();
                }
    
            }
            //遍历集合
            iterator = coll.iterator();
            while (iterator.hasNext()){
                System.out.println(iterator.next());
            }
        }

    不积跬步,无以至千里;不积小流,无以成江海。
  • 相关阅读:
    前端工程化之动态数据代理
    webapp开发之需要知道的css细节
    html-webpack-plugin详解
    file-loader引起的html-webpack-plugin坑
    浅谈react受控组件与非受控组件
    React创建组件的三种方式及其区别
    react项目开发中遇到的问题
    css伪元素:before和:after用法详解
    python之文件操作
    python之range和xrange
  • 原文地址:https://www.cnblogs.com/CCTVCHCH/p/14776333.html
Copyright © 2020-2023  润新知