从源码谈ArrayList、LinkedList、Vector三者的异同以及List接口的常用方法介绍

Collection接口 : 单列集合,用来存储一个一个的对象

List接口 : 有序的,可重复的数据。 —>“动态数组”

ArratList : 作为List接口的主要实现类,线程不安全,效率高;使用Object[] elementData对象数组粗出

LinkedList :对于频繁的插入和删除操作使用LinkeList效率高 原因:底层使用的是双向链表存储。

Vetor : 作为List接口的古老实现类;线程安全,效率低;

ArrayList的源码分析:

     1.JDK7 情况下:
     ArrayList list = new ArrayList();//空参   底层创建了长度是10的Object[]数组elementData
     list.add(123);  //elmetentDate[0] = new Integer(123)
     ....
     list.add(11);  //如果此次的添加导致底层elementData数组容量不够,则扩容。
     默认情况下,扩容为原来数组的1.5倍(int newCapacity = oldCapacity + (oldCapacity >> 1)),
      如果是addAll(coll1)的情况,可能加入的数组的长度加上原数组的长度(size + numNew)比原来数组的1.5倍还要长,
      直接newCapacity = size + numNew赋值给新数组的长度。
   ,同时需要将原有数组中数据复制(Arrays.copyOf)到新的数组中

    结论:建议开发中使用带参的构造器--->public ArrayList(int initialCapacity)直接设置底层Object[]数组elementData
          的容量


   2.JDK8 情况下有什么不同:
    ArrayList list = new ArrayList();//空参  底层Object[] elementData初始化为{},并没有创建长度为10的elementData

     list.add(123);//当第一次调用add()是,底层才创建了长度10的数组,并且 elmetentDate[0] = new Integer(123)
    ....
     后续的添加和扩容操作和JDK7   一样

      3. 小结:JDK7中的ArrayList的对象创建类似于单例中的饿汉式
           JDK8中的ArrayList的对象创建类似于单例中的懒汉式,延迟了数组的创建,节省了内存。
              为什么JDK8 要这样改? 比如:JDK7中 先创建了长度10的底层数组,要向数据库中请求第一条数据,但是要过一段时间
            才能等到第一条数据,没有节省内存。但是JDK8中底层Object[] elementData初始化为{},
            等到add()传入时再造长度10的底层数组,最后添加。

LinkedList源码分析:

 LinkedList list = new LinkedList(); 内部声明了Node类型的first和last属性,默认值为null
 list.add(1); //将123封装到Node中,创建了Node对象

  其中,Node定义为:  体现了LinkedList的双向链表
private static class Node<E> {
         E item;
        Node<E> next;
       Node<E> prev;

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
           }
         }

Vector的源码分析:

JDK7和8中通过Vector()构造器创建对象时,底层都创建了长度为10的数组
在扩容方面扩容为原来数组的2倍。

int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
                                         capacityIncrement : oldCapacity);

5.List接口中的常用方法
//void add(int index,Object ele):在index位置中插入ele元素
//boolean addAll(int index,Collection eles):从index位置开始将eles中的所有元素添加进去
//indexOf(ele) : equals() 如果不存在返回 -1
//lastIndexOf(ele): equals() 如果不存在返回 -1
//Object remove(int index): 删除指定index位置的元素,并返回此元素
//Object set(int index,Object ele):设置指定index位置的元素为ele
//List subList(int fromIndex,int toIndex):返回从fromIndex到toIndex位置的左闭右开的子集合

      总结:常用方法
      增:add(Object obj)
      删:remove(int index)/remove(Object obj)
      改:set(int index,Object ele)
      查:get(int index)
      插:add(int index,Object obj)
      长度:size()
      遍历:      ① Iterator 迭代器
                ② 增强foreach
                ③ 普通for循环

面试题:ArrayList、LinkeList、Vector三者的异同
同:三个类都实现了List接口,存储的数据的特点相同—>有序的,可重复的数据
异:见上

  /*
 * @author zhCoding
 * @create 2020-08-04 22:10
 */
public class ListTest {
    @Test
    public void test1(){

        ArrayList list = new ArrayList();
        list.add(123);
        list.add(456);
        list.add("AA");
        list.add(new Person("Jerry",12));
        list.add(456);
        System.out.println(list);

        //void add(int index,Object ele):在index位置中插入ele元素
        list.add(1,"BB");
        System.out.println(list);

        //boolean addAll(int index,Collection eles):从index位置开始将eles中的所有元素添加进去
        List list1 = Arrays.asList(1, 2, 3);
        list.addAll(list1);
        System.out.println(list);  //6+3   9
    }

    @Test
    public void test2(){
        ArrayList list = new ArrayList();
        list.add(123);
        list.add(456);
        list.add("AA");
        list.add(new Person("Jerry",12));
        list.add(456);
        System.out.println(list);

        //indexOf(ele) :  equals()   如果不存在返回  -1
        int index = list.indexOf(456);  //equals()   如果不存在返回  -1;
        System.out.println(index);

        //lastIndexOf(ele): equals()  如果不存在返回  -1
        int index1 = list.lastIndexOf(456);
        System.out.println(index1);

        //Object remove(int index): 删除指定index位置的元素,并返回此元素
        Object obj = list.remove(0);
        System.out.println(obj);
        System.out.println(list);

        //Object set(int index,Object ele):设置指定index位置的元素为ele
        list.set(1,"CC");
        System.out.println(list);

        //List subList(int fromIndex,int toIndex):返回从fromIndex到toIndex位置的左闭右开的子集合
        List subList = list.subList(2, 4);
        System.out.println(subList);
        System.out.println(list);
    }

    /*
    遍历:① Iterator 迭代器
         ② 增强foreach
         ③ 普通for循环
     */
    @Test
    public void test3(){
        ArrayList list = new ArrayList();
        list.add(123);
        list.add(456);
        list.add("AA");

        //① Iterator 迭代器
        Iterator iterator = list.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }

        //② 增强foreach
        for (Object obj : list){
            System.out.println(obj);
        }

        //③ 普通for循环
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }

}

Test1:
在这里插入图片描述
Test2:
在这里插入图片描述
Test3:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值