您好,欢迎来到飒榕旅游知识分享网。
搜索
您的当前位置:首页集合_Day01

集合_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);
        }
    }
}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- sarr.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务