02Collection

Collection体系集合

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TjmR2ERA-1637294066479)(C:\Users\17730\AppData\Roaming\Typora\typora-user-images\image-20211117222521504.png)]

  • 集合:该体系结构的根接口,代表一组对象,

  • List接口的特点:有序、有下标、元素可重复

  • Set接口的特点:无序、无下标、元素不能重复

Collection父接口
  • 特点:代表一组任意类型的对象,无序、无下标、不能重复
  • 方法:
    • boolean add(Object obj)//添加一个对象
    • boolean addAll(Collection c)//将一个集合中的所有对象添加到此集合中。
    • void clear()//清空此集合中的所有对象
    • boolean contains(Object o)//检查此集合中是否包含o对象
    • boolean equals (Object o)//比较此集合是否与指定对象相等
    • boolean isEmpty()//判断此集合是否为空
    • boolean remove(Object o)//在此集合中移除o对象
    • int size()//返回此集合中的元素个数
    • Object[] toArray()//将此集合转换成数组
package chapter01;
//需要导入一些包哦
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("A");
            collection.add("B");
            collection.add("C");
        System.out.println("元素个数"+collection.size());
        System.out.println(collection);
            //删除元素
        //collection.remove("B");
        System.out.println(collection);
        //collection.clear();//清理干净
            //遍历元素
        //3.1增强for
        for (Object object:collection){
            System.out.println(object);
        }
        //3.2使用迭代器,专门用来迭代集合 有hasNext,nest,remove三个
        Iterator iterator=  collection.iterator();//迭代器
        System.out.println(iterator.hasNext());//true
        while (iterator.hasNext()){
            String s1=(String)iterator.next();
            //collection.remove(s1);
            iterator.remove();
            System.out.println(s1);
        }
        System.out.println(collection.size());
        //判断
        System.out.println(collection.contains("D"));//是否有C
        System.out.println(collection.isEmpty());//是否是空
    }
}
Collection 的使用,保存学生信息
public class Demo02 {
    public static void main(String[] args) {
        //先新建collrctipn对象
        Collection collection = new ArrayList();
        Student s1=new Student("阿娆",20);
        Student s2=new Student("麦麦",21);
        Student s3=new Student("娟娟",22);
        Student s4=new Student("小卢",24);
        //1.添加数据
        collection.add(s1);
        collection.add(s2);
        collection.add(s3);
        System.out.println(collection.size());
        //2.删除对象
//        collection.remove(s1);
//        System.out.println(collection.size());
//        collection.clear();//只是从集合里移除,但是并没有删除
        //3.遍历对象
        //3.1增强for
        for(Object object:collection){
            Student s=(Student)object;
            System.out.println(s.toString());
        }
        //3.2迭代器hasNext       next()     remove() ;迭代过程中不能使用collection的删除方法
        Iterator iterator= collection.iterator();
        while (iterator.hasNext()){
            Student s=(Student)iterator.next();
            System.out.println(s.toString());
        }
        //判断
        System.out.println(collection.contains(s1));
        System.out.println(collection.contains(s4));
    }
}
package chapter01;

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

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

    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 toString(){
        return "Student[name="+name+", age ="+age+"]";
    }
}

Student[name=麦麦, age =21]
Student[name=娟娟, age =22]
Student[name=阿娆, age =20]
Student[name=麦麦, age =21]
Student[name=娟娟, age =22]
true
false

1]
Student[name=娟娟, age =22]
Student[name=阿娆, age =20]
Student[name=麦麦, age =21]
Student[name=娟娟, age =22]
true
false

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值