一、ArrayList源码分析

一、ArrayList源码分析

java集合源码

最近看了下java集合源码,特将此记录下来(基于jdk1.7)
    ArrayList 继承了AbstractList抽象类,
    ArrayList 实现了List接口
    ArrayList 实现了RandomAccess接口,使之具有随机访问的特性
    ArrayList 实现了Cloneable接口 ,可以克隆
    ArrayList 实现了Serializable接口,可以进行序列化和反序列化操作,可以在网络上进行传输和存储

ArrayList中实际存储数据的是数组 elementData,数组的长度就是集合的大小, 不参与序列化过程

private transient Object[] elementData;

集合大小size

private int size;

ArrayList构造函数.默认的集合的大小为10

public ArrayList(int initialCapacity) {
        super();
        //长度<0抛出非法异常
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        this.elementData = new Object[initialCapacity];
    }
public ArrayList() {
        this(10);
    }
public ArrayList(Collection<? extends E> c) {
        elementData = c.toArray();
        size = elementData.length;
        //c.toArray() 返回的对象可能不是Object[]对象,如果不是需要进行转换
        if (elementData.getClass() != Object[].class)
            elementData = Arrays.copyOf(elementData, size, Object[].class);
    }

ArrayList增加元素

    //增加单个元素
    public boolean add(E e) {
        //增加元素后是否超过集合大小,如果超过,进行扩容
        ensureCapacityInternal(size + 1);  
        //直接在后面添加元素
        elementData[size++] = e;
        return true;
    }
    private void ensureCapacityInternal(int minCapacity) {
        modCount++;
        //增加后超过当前数长度,进行扩容
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }
    private void grow(int minCapacity) {
        int oldCapacity = elementData.length;
        //新容量=旧容量+旧容量无符号右移1位(默认10 无符号右移1位就是5) 以前是 新容量= (旧容量*3/2+1)
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        //如果扩容后新容量<最少容量,则将最少容量赋值给新容量
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        //将数组全部赋值新容量的数组中
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

    //在指定位置增加元素
     public void add(int index, E element) {
        //判断是否越界
        rangeCheckForAdd(index);

        ensureCapacityInternal(size + 1); 
        //将此索引对应的位置向后移动一位
        System.arraycopy(elementData, index, elementData, index + 1,
                         size - index);
        elementData[index] = element;
        size++;
    }
    //增加集合到集合中
     public boolean addAll(Collection<? extends E> c) {
        Object[] a = c.toArray();
        int numNew = a.length;
        ensureCapacityInternal(size + numNew); 
        System.arraycopy(a, 0, elementData, size, numNew);
        size += numNew;
        return numNew != 0;
    }
    //指定位置添加集合
     public boolean addAll(int index, Collection<? extends E> c) {
        rangeCheckForAdd(index);

        Object[] a = c.toArray();
        int numNew = a.length;
        ensureCapacityInternal(size + numNew);  
        int numMoved = size - index;
        if (numMoved > 0)
            System.arraycopy(elementData, index, elementData, index + numNew,
                             numMoved);
        System.arraycopy(a, 0, elementData, index, numNew);
        size += numNew;
        return numNew != 0;
    }

ArrayList查询元素

  public E get(int index) {
        rangeCheck(index);
        //直接取根据数组方式获取 索引获取
        return elementData(index);
    }

ArrayList删除元素

     //根据索引删除
     public E remove(int index) {
        rangeCheck(index);
        modCount++;
        //记录旧值
        E oldValue = elementData(index);
        int numMoved = size - index - 1;
        //如果不是最后一个元素,则需要将索引后面的元素向前移一位,最后一个元素空出
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData,index,numMoved);
        //设置最后一个元素为空,让虚拟机垃圾回收掉
        elementData[--size] = null; 
        //返回删除的旧值
        return oldValue;
    }
    //根据对象删除
    public boolean remove(Object o) {
        if (o == null) {
            for (int index = 0; index < size; index++)
                if (elementData[index] == null) {
                    fastRemove(index);
                    return true;
                }
        } else {
            for (int index = 0; index < size; index++)
                if (o.equals(elementData[index])) {
                    fastRemove(index);
                    return true;
                }
        }
        return false;
    }
    //删除索引之间的元素(包括fromIndex 不包括toIndex)
   protected void removeRange(int fromIndex, int toIndex) {
        modCount++;
        int numMoved = size - toIndex;
        System.arraycopy(elementData, toIndex, elementData, fromIndex,
                         numMoved);

        // Let gc do its work
        int newSize = size - (toIndex-fromIndex);
        while (size != newSize)
            elementData[--size] = null;
    }

ArrayList清除元素

      public void clear() {
        modCount++;
        for (int i = 0; i < size; i++)
            elementData[i] = null;

        size = 0;
    }

总结:

  1. ArrayList 底层是基于数组来实现的,可以通过下标准确的找到目标元素 因此查找的效率高;但是添加或删除元素会涉及到大量元素的位置移动,效率很低
  2. ArrayList有三种构造函数,默认的是可以添加元素大小为10,如果不够会自动扩容,即生成一个新的数组,方法 新=旧+(旧>>1) 使用右移可以提高扩容效率
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值