java集合中Collection接口和List接口的遍历方式

一、Collection接口的遍历方式

  1. 增强for遍历
  2. 迭代器Iterator遍历
Collection<Student> collection = new ArrayList<Student>();
        Student stu1 = new Student("张三", "男");
        Student stu2 = new Student("李四", "男");
        Student stu3 = new Student("王五", "男");
        Student stu4 = new Student("赵六", "男");
        Student stu5 = new Student("王丽", "女");
        Student stu6 = new Student("王丽", "女");
        // 添加
        collection.add(stu1);
        collection.add(stu2);
        collection.add(stu3);
        collection.add(stu4);
        collection.add(stu5);
        collection.add(stu6);
        System.out.println(collection);
        System.out.println(collection.size());
        //删除
        collection.remove(new Student("王丽", "女"));
        System.out.println(collection.size());
        //遍历
        // 1.foreach
        for (Student student : collection) {
            System.out.println(student);
        }
        System.out.println("-----------------------");
        // 2. iterator
        Iterator<Student> iterator = collection.iterator();
        while(iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        //判断包含
        System.out.println(collection.contains(stu1));
        //如果要让判断new的名字和性别都相同的对象为同一个对象,可以在Student类中重写Object类的equals方法

二、List接口的遍历方式

  1. ArrayList接口
    1. 增强for遍历
    2. 普通for循环
    3. 迭代器Iterator遍历
    4. 列表迭代器ListIterator遍历
    ArrayList<String> arrayList = new ArrayList<>();

        // 添加
        arrayList.add("苹果");
        arrayList.add("西瓜");
        arrayList.add("橙子");
        arrayList.add("香蕉");
        arrayList.add("柠檬");
        // 删除
        // arrayList.remove(new String("苹果"));
            System.out.println(arrayList);

        // 遍历
        // 1.for
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.println(arrayList.get(i));
        }
        System.out.println("--------------------------");
        // 2.foreach
        for (String string : arrayList) {
            System.out.println(string);
        }
        System.out.println("---------------------------");
        // 3.Iterator
        Iterator<String> iterator = arrayList.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        System.out.println("----------------------------");
        // 4.ListIterator
        ListIterator<String> listIterator = arrayList.listIterator();
        while (listIterator.hasNext()) {
            System.out.println(listIterator.next());
        }
        System.out.println("----------------------------------");
        while (listIterator.hasPrevious()) {
            System.out.println(listIterator.previous());
        }
        // 包含
        System.out.println(arrayList.contains("苹果"));
        System.out.println(arrayList.contains(new String("苹果")));
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值