集合_Day01

集合

  • 集合用于存储数量不等的多个对象,还可以用于保存有映射关系的关联数组(存储是指在内存层面的)

Collection接口

|------Collection接口 : 单列集合,用来存储一个一个的对象
       |------List   存储有序,重复的
            |----- ArrayList (线程不安全, 效率高,底层使用Object[]存储)
            |----- LinkedList (底层使用双向链表,对于频繁的插入,删除操作,使用LinkedList效率比ArrayList高)
            |----- Vector (线程安全,效率低,底层使用Object[]存储)
       |------Set    存储无序,不可重复的
            |----- HashSet,LinkedHashSet,TreeSet

List (元素有序,可以重复)

ArrayList
JDK7.0
  • 底层创建了长度为10的Object[]数组elementData
  • 如果在添加元素时elementData数组容量不够,则需要扩容
  • 扩容,默认扩容为原来容量的1.5倍,同时将原来数组中的数据复制到新的数组中
JDK8.0
  • 底层Object[] element初始化为{}
  • 第一次使用add()方法时才创建长度为10的数组,并将数组添加带新创建的数组中
  • 扩容操作与JDK7.0一致
LinkedList
  • 内部声明了Node 类型的first和last属性,默认为NUll
  • 将元素封装到Node中,创建Node对象,定义了静态内部类,实现双向链表
Vector
  • 在JDK7和JDK8创建对象时,底层都创建了长度为10的数组
  • 扩容为旧数组的两倍

List常用方法

  • void add(int index, Object ele):在index位置插入ele元素
  • boolean addAll(int index, Collection eles):从index位置开始将eles中的所有元素添加进来
  • Object get(int index):获取指定index位置的元素
  • int indexOf(Object obj):返回obj在集合中首次出现的位置
  • int lastIndexOf(Object obj):返回obj在当前集合中末次出现的位置
  • Object remove(int index):移除指定index位置的元素,并返回此元素
  • Object set(int index, Object ele):设置指定index位置的元素为ele
  • List subList(int fromIndex, int toIndex):返回从fromIndex到toIndex位置的子集合
public class Test1 {
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();
        arrayList.add("1");
        arrayList.add("2");
        arrayList.add("3");
        arrayList.add("4");
        ArrayList arrayList1 = new ArrayList();
        arrayList1.add("A");
        arrayList1.add("B");
        //void add(int index, Object ele)
        arrayList.add(0,"a");
        //boolean addAll(int index, Collection eles)
        arrayList.addAll(arrayList1);
        //Object get(int index)
        System.out.println(arrayList.get(0));
        //int indexOf(Object obj)
        System.out.println(arrayList.indexOf("1"));
        //int lastIndexOf(Object obj)
        System.out.println(arrayList.lastIndexOf("2"));
        //Object remove(int index)
        Object o = arrayList.remove("a");
        System.out.println(o);
        //Object set(int index, Object ele)
        arrayList.set(0,'C');
        //List subList(int fromIndex, int toIndex)
        List list = arrayList.subList(2,4);
        System.out.println(arrayList);
        System.out.println(list);
    }
}

Iterator迭代器

        //Iterator迭代器接口 (游标指着第一个元素之前)
        // hasNext()是判断是否还存在下一个元素
        // next(),1.指针下移,2.返回指针标记的元素
        // remove() , 移除当前指针指向的元素
        Iterator i = collection.iterator();

        while (i.hasNext()){
            if ("DD".equals(i.next())){
                i.remove();
            }
        }
        System.out.println(collection);
    }

foreach循环

  • 格式如下
for (对象类型 临时变量 : 集合) {
    System.out.println(临时变量);
}
  • 其底层还是使用的Iterator迭代器
  • 不能在foreach循环代码块中修改元素的值,因为是元素的值传递了一次,我们改的是临时变量的值
public class Test2 {
    public static void main(String[] args) {
        Collection collection = new ArrayList();
        collection.add("1");
        collection.add("2");
        collection.add("3");
        collection.add("4");
        for (Object o : collection) {
            System.out.println(o);
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

临水而愚

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

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

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

打赏作者

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

抵扣说明:

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

余额充值