Java学习笔记117——迭代器Iterator

迭代器

迭代器有自己的数据类型Iterator类型

Iterator iterator() 迭代器,集合的专用遍历方式

boolean hasNext() 如果迭代具有更多元素,则返回 true 。

Object next() 返回迭代中的下一个元素。

public class CollectionDemo5 {
    public static void main(String[] args) {
        //创建集合对象
        Collection c1 = new ArrayList();
​
        //向集合中添加元素
        c1.add("hello");
        c1.add("world");
        c1.add("java");
        c1.add("hadoop");
​
        //获取c1的迭代器对象
        Iterator iterator = c1.iterator();
        //java.util.ArrayList$Itr@4554617c
//        System.out.println(iterator);
​
        //Object next() 返回迭代中的下一个元素。
//        Object obj = iterator.next();
//        System.out.println(obj);
//        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());
        //我们通过观察发现,最后一个System.out.println(iterator.next());
        //是多余的,因为此刻已经将迭代器中的元素遍历完了,不应该写
        //我们实际应该在获取之前判断一下下一个位置上是否有元素,如果有就next()获取
        //如果没有就不获取
        //boolean hasNext() 如果迭代具有更多元素,则返回 true 。
//        if(iterator.hasNext()){
//            System.out.println(iterator.next());
//        }
//
//        if(iterator.hasNext()){
//            System.out.println(iterator.next());
//        }
//
//        if(iterator.hasNext()){
//            System.out.println(iterator.next());
//        }
//
//        if(iterator.hasNext()){
//            System.out.println(iterator.next());
//        }
//
//        if(iterator.hasNext()){
//            System.out.println(iterator.next());
//        }
//
//        if(iterator.hasNext()){
//            System.out.println(iterator.next());
//        }
//
//        if(iterator.hasNext()){
//            System.out.println(iterator.next());
//        }
​
        //通过加入了判断我们发现,虽然代码不报错,也可以将结果元素正确的打印
        //但是呢,我们并不知道迭代器什么时候遍历到最后一个元素
        //怎么改进呢?用循环改进
        //由于我们不知道什么时候结束循环,使用while循环
        while (iterator.hasNext()) {
            Object next = iterator.next();
​
            //向下转型
            String s = (String) next;
            System.out.println(s + ",长度为:" + s.length());
        }
    }
}

1、能否将while循环改成普通for循环呢?能,但是不推荐,推荐使用whil循环

2、为什么将Iterator一个接口呢?而不是一个类呢? 将来你需要根据不同的数据创建不同的集合进行存储,每个集合都有自身独有特点,很有可能每一个 集合遍历的顺序特点不一样,所以取值的方式也很有可能不一样,所以不应该直接实现,而是通过一个接口 将来特有的集合要去使用迭代器时候,就实现一下这个接口,添加自己特有的遍历元素逻辑代码。

public class CollectionDemo6 {
    public static void main(String[] args) {
        //创建学生集合对象
        Collection c1 = new ArrayList();
​
        //创建学生对象
        Student s1 = new Student("张咪", 16);
        Student s2 = new Student("张梦云", 17);
        Student s3 = new Student("刘梦云", 18);
​
        //将学生对象添加到集合中
        c1.add(s1);
        c1.add(s2);
        c1.add(s3);
​
        //获取迭代器对象
        Iterator iterator = c1.iterator();
​
        //遍历迭代器对象,获取迭代器中的每个元素
        while (iterator.hasNext()) {
            Object next = iterator.next();
​
//            //向下转型
//            Student s = (Student) next;
//            System.out.println(s.getName() + "--" + s.getAge());
            System.out.println(((Student) next).getName() + "--" + ((Student) next).getAge());
        }
​
        //NoSuchElementException
//        Object next = iterator.next();
//        System.out.println(((Student) next).getName() + "--" + ((Student) next).getAge());
​
​
​
​
//        System.out.println("===========用普通for循环遍历(不推荐)======================");
//
//        Iterator iterator = c1.iterator();
//        for(;iterator.hasNext();){
//            Object next = iterator.next();
//
////            //向下转型
//            Student s = (Student) next;
//            System.out.println(s.getName() + "--" + s.getAge());
//        }
    }
}

 

案例

需求:将5个学生对象添加到集合中并遍历 Collection集合编写代码,完整代码1.0版本

public class CollectionDemo7 {
    public static void main(String[] args) {
        //创建学生集合对象
        Collection c1 = new ArrayList();
​
        //创建学生对象
        Student s1 = new Student("张咪", 16);
        Student s2 = new Student("张梦云", 17);
        Student s3 = new Student("刘梦云", 18);
        Student s4 = new Student("贝贝", 20);
        Student s5 = new Student("姜水旺", 21);
​
        //将学生对象添加到集合中
        c1.add(s1);
        c1.add(s2);
        c1.add(s3);
        c1.add(s4);
        c1.add(s5);
​
        //获取集合中的迭代器对象
        Iterator iterator = c1.iterator();
​
        //遍历迭代器元素
        while (iterator.hasNext()) {
            Object next = iterator.next();
            //向下转型
            Student s = (Student) next;
            System.out.println(s.getName() + "---" + s.getAge());
        }
    }
}
posted @ 2021-12-19 00:06  欧朋  阅读(58)  评论(0)    收藏  举报