集合的遍历迭代器的最开始学习

集合转数组

把集合转成数组,可以实现集合的遍历

public class CollectionDemo3 {
    public static void main(String[] args) {
        Collection c = new ArrayList();
        c.add("hello");//object obj = "hello";向上转型
        c.add("world");
        c.add("java");

        //把集合转成数组,可以实现集合的遍历
        Object[] objects = c.toArray();
        for (int i = 0; i < objects.length; i++){
            //Object没有length方法,要想使用字符串的方法,就必须把元素还原成字符串
            //向下转型
            String s = (String) objects[i];
            System.out.println(s + "---" + s.length());
        }
    }
}

Iterator iterator() 迭代器

集合专用的遍历方式

public class IteratorDemo {
    public static void main(String[] args) {
        Collection c = new ArrayList();
        c.add("hello");
        c.add("world");
        c.add("java");

        //Iterator是接口,返回的是子类对象,多态
        //Iterator iterator() 迭代器,集合专用的遍历方式
        Iterator it = c.iterator();
        //Object next()获取元素,并移动到下一个位置
        System.out.println(it.next());
        System.out.println(it.next());
        System.out.println(it.next());
    }
}

在这里插入图片描述
如果多一个next,会报错,NoSuchElementException没有这样的元素,因为已经到最后了

public class IteratorDemo {
    public static void main(String[] args) {
        Collection c = new ArrayList();
        c.add("hello");
        c.add("world");
        c.add("java");

        //Iterator是接口,返回的是子类对象,多态
        //Iterator iterator() 迭代器,集合专用的遍历方式
        Iterator it = c.iterator();
        //Object next()获取元素,并移动到下一个位置
        System.out.println(it.next());
        System.out.println(it.next());
        System.out.println(it.next());
        System.out.println(it.next());
    }
}

在这里插入图片描述
因为需要加一个判断,在每次获取之前,如果有一个判断就好了
判断是否有下一个元素,有就获取,没有就不搭理

while循环

public class IteratorDemo {
    public static void main(String[] args) {
        Collection c = new ArrayList();
        c.add("hello");
        c.add("world");
        c.add("java");
        
        Iterator it = c.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
    }
}

在这里插入图片描述
注意it.next返回的是对象,所以进行一下向下转型才行,转换成string

public class IteratorDemo {
    public static void main(String[] args) {
        Collection c = new ArrayList();
        c.add("hello");
        c.add("world");
        c.add("java");
             
        Iterator it = c.iterator();
        while (it.hasNext()){
            String s = (String) it.next();
            System.out.println(s);
        }
    }
}

for循环

注意for循环效率高,但是看起来不得劲,还是while比较好
不要多次使用it.next(),因为每次使用都是访问一个对象

public class IteratorDemo {
    public static void main(String[] args) {
        Collection c = new ArrayList();
        c.add("hello");
        c.add("world");
        c.add("java");
       //for循环改写       
        for (Iterator it = c.iterator(); it.hasNext();){
            String s = (String) it.next();
            System.out.println(s);
        }
    }
}

总结:

在这里插入图片描述
迭代器为什么不定义一个类,而是一个接口?
在这里插入图片描述

迭代器原理与源码

编译看左边,运行看右边

练习题:

需求:储存自定义对象并遍历student(name,age)

学生类

public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

/**
 * 需求:储存自定义对象并遍历student(name,age)
 *
 * 分析:
 *      A:创建学生类
 *      B:创建集合对象
 *      C:创建学生对象
 *      D:把学生对象添加到集合对象中
 *      E:遍历集合
 *
 * @author yhs
 * @date 2020/7/23 - 14:40
 */
public class CollectionTest2 {
    public static void main(String[] args) {
        //创建集合对象
        Collection c = new ArrayList();

        //创建学生对象
        Student s1 = new Student("貂蝉",25);
        Student s2 = new Student("小乔",16);
        Student s3 = new Student("鸡蛋糕",26);
        Student s4 = new Student();
        s4.setName("大乔");
        s4.setAge(22);

        //把学生对象添加到集合对象中
        c.add(s1);
        c.add(s2);
        c.add(s3);
        c.add(s4);

        //遍历集合
        Iterator it = c.iterator();
        while (it.hasNext()){
            Student s = (Student)it.next();
            System.out.println(s.getName() + "---" + s.getAge());
        }
    }
}

在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值