java:Collection体系结构

Collection体系集合

在这里插入图片描述

  • Collection表示该体系结构的根接口,代表一组对象,称为“集合”
  • 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 gather;

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

/*
Collection接口的使用
(1)添加元素
(2)删除元素
(3)遍历元素
(4)判断元素是否存在
 */
public class Demo01 {
    public static void main(String[] args) {
        //创建一个集合
        Collection collection = new ArrayList();
//        (1)添加元素
        collection.add("苹果");
        collection.add("西瓜");
        collection.add("榴莲");
        System.out.println("元素个数"+collection.size());
        System.out.println(collection);
//        (2)删除元素
//        collection.remove("榴莲");
//        System.out.println("删除之后"+collection.size());
//        collection.clear();
//        System.out.println("删除之后"+collection.size());
//        (3)遍历元素【重点】
        //3.1使用增强for
        System.out.println("----------");
        for (Object object:collection) {
            System.out.println((String)object);
        }
        //3.2使用迭代器(专门用来遍历集合的一种方式)
        //hasNext();有没有下一个元素
        //next();获取下一个元素
        //remove();删除当前元素
        Iterator it = collection.iterator();
        while(it.hasNext()){
            Object object = it.next();
            System.out.println(object);
 //           collection.remove(object);//迭代器运行过程中不能用collection的删除方法
            it.remove();//可以用迭代器的删除方法
        }
        System.out.println(collection.size());

//       (4)判断元素是否存在
        System.out.println(collection.contains("西瓜"));//false
        System.out.println(collection.isEmpty());//true
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RaCT0IpR-1649392626287)(C:\Users\孙甲浩\Desktop\图片\迭代器.png)]

package gather;

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

/*
Collection:保存学生信息
 */
public class Demo02 {
    public static void main(String[] args) {
        //新建Collection对象
        Collection collection = new ArrayList();
        Student s1 = new Student("张三",20);
        Student s2 = new Student("张无忌",18);
        Student s3 = new Student("李四",22);
        //1.添加学生数据
        collection.add(s1);
        collection.add(s2);
        collection.add(s3);
        collection.add(s3);//List元素可以重复
        System.out.println("元素个数"+collection.size());
        System.out.println(collection);
        //2.删除
        collection.remove(s1);
        collection.remove(new Student("李四",22));//这样不能删除对象,相当于在集合外面添加了一个对象,不在集合里面,不能删除
//        collection.clear();//清除的是元素信息,不是对象
        System.out.println("删除之后"+collection.size());
        //3.遍历
        //3.1增强for
        System.out.println("-------------");
        for (Object object: collection) {
            System.out.println(object);
        }
        //3.2迭代器:hasNext()  next();  remove();  迭代过程中不能使用collection的删除方法
        System.out.println("-------------");
        Iterator it = collection.iterator();
        while(it.hasNext()){
           Student s =(Student) it.next();
            System.out.println(s);
        }
        //4.判断
        System.out.println(collection.contains(s1));//false
        System.out.println(collection.isEmpty());//false
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nWCjCZHW-1649392626288)(C:\Users\孙甲浩\Desktop\图片\分析.png)]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_ψ(`∇´)ψ_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值