学习博客:集合框架之collection的使用

集合即对象的容器,定义了对多个对象进行操作的常用方法,可实现数组的功能

在这里插入图片描述

与数组的区别:

1.数组长度固定,集合长度不固定
2.数组可以存储基本类型和引用类型,集合只能存储引用类型

导入包

import java.util.*;

对元素进行操作

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Demo01 {
    public static void main(String[] args) {
        //创建集合
        Collection collection = new ArrayList();

        //添加元素
        collection.add("vivo");
        collection.add("oppo");
        collection.add("lenovo");
        System.out.println(collection.size());
        System.out.println(collection);

        //删除元素
//        collection.remove("lenovo");
//        System.out.println("删除后:" + collection);
//        collection.clear();
//        System.out.println(collection.size());

        //遍历元素
        //增强for
        System.out.println("增强for:");
        for (Object object: collection) {
            System.out.println(object);
        }

        //迭代器 hasNext()判断有无下一个元素  next()获取下一个元素  remove()删除当前元素
        Iterator it = collection.iterator();
        System.out.println("迭代器:");
        while(it.hasNext()){
           String str = (String)it.next();
            System.out.println(str);
            //迭代器中不允许使用collection删除方法,否则出现ConcurrentModificationException异常
            //可以使用it.remove()
        }

        //判断
        System.out.println(collection.contains("vivo"));    //是否包含"vivo"
        System.out.println(collection.isEmpty());   //是否为空
    }
}

在这里插入图片描述
对对象进行操作
先创建一个Animals类

public class Animals {
    private String name;
    private int age;
    private String shout;

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

    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;
    }

    public String getShout() {
        return shout;
    }

    public void setShout(String shout) {
        this.shout = shout;
    }

    //重写toString方法
    @Override
    public String toString() {
        return "Animals{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", shout='" + shout + '\'' +
                '}';
    }
}

collection的一些方法,添加、删除、遍历

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Demo02 {
    public static void main(String[] args) {
        //新建Collection对象
        Collection collection = new ArrayList();
        Animals cat = new Animals("猫",1,"miao");
        Animals dog = new Animals("狗",1,"wang");
        Animals sheep = new Animals("羊",1,"mie");

        //添加数据
        collection.add(cat);
        collection.add(dog);
        collection.add(sheep);
        System.out.println(collection.size());
        System.out.println(collection.toString());

        //删除
//        collection.remove(cat);
//        System.out.println("删除后:" + collection.size());
//        collection.clear();     //清空collection集合内的地址,对象没有被删除
//        System.out.println("清空后:" + collection.size());

        //遍历
        for (Object object: collection) {
            Animals a = (Animals) object;
            System.out.println(a.toString());
        }

        //迭代器
        Iterator it = collection.iterator();
        System.out.println("迭代器:");
        while(it.hasNext()){
            Animals s = (Animals)it.next();
            System.out.println(s);
        }

        System.out.println(collection.contains(new Animals("猫",1,"miao")));
        System.out.println(collection.isEmpty());
    }
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值