zoukankan      html  css  js  c++  java
  • 迭代器Iterator接口

    集合元素的遍历操作,使用迭代器Iterator接口

    • 1.内部的方法:hasNext() 和 next()
    • 2.集合对象每次调用iterator()方法都得到一个全新的迭代器对象,
    • 默认游标都在集合的第一个元素之前。
    • 3.内部定义了remove(),可以在遍历的时候,删除集合中的元素。此方法不同于集合直接调用remove()
    public class IteratorTest {
        @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(obj.equals("Tom")){
                    iterator.remove();
    //                iterator.remove();
                }
    
            }
            //遍历集合
            iterator = coll.iterator();
            while (iterator.hasNext()){
                System.out.println(iterator.next());
            }
        }
    }
    
    会当凌绝顶,一览众山小
  • 相关阅读:
    记一次vue.js用 http.post 前端传json到后台用javabean接收的坑
    springboot1.5.x 测试sample
    sqlserver 查询表缺失索引
    Docker swarm上线的一些问题
    数据库日志文件压缩
    Docker 挂载
    单播广播和多播
    导入数据库表后某些字段的精度为0
    Source Qualifiter组件中Sql Query属性的脚本返回结果集的列数大于组件定义的数量
    distinct和order by冲突
  • 原文地址:https://www.cnblogs.com/leyzzz/p/15333369.html
Copyright © 2011-2022 走看看