集合1

  • 为什么出现集合类?
    面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,我们就需要对这多个对象进行存储。
    数组长度固定,不能适应变化的需求,所以,Java就提供了集合类供我们使用。

Collction体系结构图:
这里写图片描述

1.Collection
是集合的顶层结构,定义了集合的共性功能。
可以存储对象,是接口。
成员方法:
A:添加功能
* boolean add(Object obj):往集合中添加一个元素
* boolean addAll(Collection c):往集合中添加多个元素
B:删除功能
* void clear():清空所有元素
* boolean remove(Object o):从集合中删除一个元素
* boolean removeAll(Collection c):从集合中删除另一个集合的元素
C:判断功能
* boolean contains(Object o):判断集合中是否包含指定的元素
* boolean containsAll(Collection c):判断集合中是否包含另一个集合的元素
* boolean isEmpty():判断集合是否为空。
D:交集功能
* boolean retainAll(Collection c)
E:迭代器(集合特有的遍历方式)
* Iterator iterator()
F:长度功能
* int size():返回集合中元素的个数

public static void main(String[] args) {

        Collection<String> c= new ArrayList<String>();

        //增加功能
        c.add("terrible");     
        c.add("Demo");
        for (String string : c) {
            System.out.println(string);
        }

        //删除功能
        c.remove("Demo");       
        for (String string : c) {
            System.out.println(string);
        }

        //判断功能
        System.out.println(c.contains("Demo"));    
        System.out.println(c.isEmpty());
        for (String string : c) {
            System.out.println(string);
        }
        System.out.println("----------------");

        Collection<String> c2 = new ArrayList<String>();
        c2.add("dsdsd");

        System.out.println(c);
        System.out.println(c2);
        System.out.println("----------------");

        for (String string : c2) {
            System.out.println(string);
        }
        System.out.println(c.retainAll(c));   //判断交集功能

        for (String string : c) {
            System.out.println(string);
        }
    }

2.List
(1)List集合的元素有序(存储和取出顺序一致),元素可重复

(2)List的特有功能:
A:添加功能
void add(int index,Object obj):在指定的位置添加元素
B:删除功能
Object remove(int index):通过指定的索引删除元素,并把删除的元素返回
C:获取功能
get(int index) 返回列表中指定位置的元素。
D:替换功能
Object set(int index,Object obj)

public static void main(String[] args) {
        List l= new ArrayList();

        //添加功能
        l.add(0, "wow~");          
        l.add(1, "DPS");
        l.add(2, "tank");
        for (Object obj : l) {
            System.out.println(obj);
        }

        //删除功能
        l.remove(1);
        for (Object obj : l) {
            System.out.println(obj);
        }

        //获取功能
        System.out.println(l.get(1));

        //替换功能
        l.set(1, "pretty boy");
        for (Object obj : l) {
            System.out.println(obj);
        }
    }

List的迭代器遍历法:

public static void main(String[] args) {
        //A:存储字符串并遍历

        //创建字符串
        String s1="Hi~";
        String s2="happy";
        String s3="dog";

        //创建ArrayList并将字符串放进去
        ArrayList<String> al = new ArrayList<String>();
        al.add(0, s1);
        al.add(1, s2);
        al.add(2, s3);

        //使用迭代器便利
        Iterator<String> it = al.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }

ArrayList与LinkedList的相同点与不同点
相同点:有顺序的,元素可以重复
(1)ArrayList特点:
底层数据结构是数组,查询快,增删慢
(2)LinkedList特点:
底层数据结构是链表,查询慢,增删快

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值