ArrayList源码解析 基于jdk1.8

ArrayList源码解析

ArrayList继承结构开始分析


public class ArrayList extends AbstractList
        implements List, RandomAccess, Cloneable, java.io.Serializable

ArrayList是继承于AbstractList的线性表。
RandomAccess 是一个标记接口,实现该接口让某些算法更加高效。
Cloneable是一个标记接口,重写clone方法克隆,实现浅拷贝或深拷贝。
java.io.Serializable是一个序列化接口

ArrayList属性


    //默认容量
    private static final int DEFAULT_CAPACITY = 10;

    //空数组用于赋值操作
    private static final Object[] EMPTY_ELEMENTDATA = {};

    //用于存储数据数组
    transient Object[] elementData;

    //存储元素的长度
    private int size;

关键字transient的字段生命周期存于内存中,不会写到磁盘持久化。
DEFAULT_CAPACITY是默认容量,空构造方法时是的默认容量。

ArrayList构造方法


//对指定长度的arraylist初始化
 public ArrayList(int initialCapacity) {
        super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        this.elementData = new Object[initialCapacity];
    }
//空参数初始化  默认用EMPTY_ELEMENTDATA赋值
    public ArrayList() {
        super();
        this.elementData = EMPTY_ELEMENTDATA;
    }
//复制操作,Collection里面的值通过Arrays.copyOf方法copy到arrayList
    public ArrayList(Collection c) {
        elementData = c.toArray();
        size = elementData.length;
        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;
    }
    
public void add(int index, E element) {
        if (index > size || index < 0)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));

        ensureCapacityInternal(size + 1);  // Increments modCount!!
        System.arraycopy(elementData, index, elementData, index + 1,
                         size - index);//将element的部分复制到elementData中
        elementData[index] = element;
        size++;
    }

add方法调用时通过ensureCapacityInternal方法 默认长度是否大于数据长度 如果大于长度 最终调用grow方法 扩展数据

ensureCapacityInternal方法
private void ensureCapacityInternal(int minCapacity) {
        if (elementData == EMPTY_ELEMENTDATA) {
            minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
        }

        ensureExplicitCapacity(minCapacity);
    }
private void ensureExplicitCapacity(int minCapacity) {
        modCount++;

        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }
    
grow方法

数组长度扩容操作,扩容成当前长度的1.5倍,并且复制当前数据

private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        // minCapacity is usually close to size, so this is a win:
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

ArrayList删除操作

 public E remove(int index) {
        if (index >= size)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));

        modCount++;
        E oldValue = (E) elementData[index];

        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);//重新复制操作
        elementData[--size] = null; //清理用gc进行

        return oldValue;
    }

ArrayList更改操作


public E set(int index, E element) {
        if (index >= size)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
        E oldValue = (E) elementData[index];
        elementData[index] = element;
        return oldValue;
    }

ArrayList获取操作


public E get(int index) {
        if (index >= size)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
        return (E) elementData[index];
    }

总结


1.ArrayList是线程是不同步的会有线程安全问题。
2.数据增长时,如果超过内部数组长度的时候需要扩展数组长度的1.5倍。
3.插入删除数据会有数据复制操作,造成性能消耗。
4.get和set方法直接直接在数组获取。

你可能感兴趣的:(ArrayList源码解析 基于jdk1.8)