ArrayList源码解析

ArrayList是常用的容器类,大家都知道随机访问效率高,并发不安全,一般多线程的情况都不考虑它。但是如果有人问你为什么不安全,会出现哪些异常。可能不熟悉源码的话不能顺畅回答出来。
下面我们根据源码分析下它的技术实现细节。

ArrayList是一个泛型类,继承AbstractList 实现了List等接口,默认大小10,elementData用来存储数据,size记录存储数据的个数。静态常量EMPTY_ELEMENTDATA和DEFAULTCAPACITY_EMPTY_ELEMENTDATA分别分配给空集合。

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
    private static final long serialVersionUID = 8683452581122892189L;

    private static final int DEFAULT_CAPACITY = 10;

   
    private static final Object[] EMPTY_ELEMENTDATA = {};

  
    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};

   
    transient Object[] elementData; 

  
    private int size;

三个构造函数,
第一个ArrayList(int initialCapacity)指定初始化大小,负数时抛出异常,大于0,按指定大小申请数组,等于0,分配指向EMPTY_ELEMENTDATA。

    public ArrayList(int initialCapacity) {
        if (initialCapacity > 0) {
            this.elementData = new Object[initialCapacity];
        } else if (initialCapacity == 0) {
            this.elementData = EMPTY_ELEMENTDATA;
        } else {
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        }
    }

第二个无参构造函数分配指向DEFAULTCAPACITY_EMPTY_ELEMENTDATA。

    public ArrayList() {
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }

第三个用一个集合做入参,先调用c.toArray新申请一个数组,接着Arrays.copyOf 浅拷贝填充引用。

    public ArrayList(Collection<? extends E> c) {
        elementData = c.toArray();
        if ((size = elementData.length) != 0) {
            // c.toArray might (incorrectly) not return Object[] (see 6260652)
            if (elementData.getClass() != Object[].class)
                elementData = Arrays.copyOf(elementData, size, Object[].class);
        } else {
            // replace with empty array.
            this.elementData = EMPTY_ELEMENTDATA;
        }
    }

当ArrayList经历扩容,大量的删除后,需要根据现在的size,去收回内存。重新申请一块同现在size的数组存放数据,

  public void trimToSize() {
        modCount++;
        if (size < elementData.length) {
            elementData = (size == 0)
              ? EMPTY_ELEMENTDATA
              : Arrays.copyOf(elementData, size);
        }
    }

返回大小和判空

  public int size() {
        return size;
    }
    public boolean isEmpty() {
        return size == 0;
    }

indexOf方法返回给定对象在ArrayList中的索引位置。为空时,遍历集合找到第一个为空的位置。非空时,逐个对比。没有找到默认返回-1.
contains 调用indexOf ,返回位置大于等于0,代表包含该对象。

    public boolean contains(Object o) {
        return indexOf(o) >= 0;
    }

    public int indexOf(Object o) {
        if (o == null) {
            for (int i = 0; i < size; i++)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = 0; i < size; i++)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }

lastIndexOf 与indexOf 唯一的不同就是反序遍历。

  public int lastIndexOf(Object o) {
        if (o == null) {
            for (int i = size-1; i >= 0; i--)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = size-1; i >= 0; i--)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }

重新申请数组,复制引用值,但是不复制内部引用对象,属于浅拷贝。

   public Object clone() {
        try {
            ArrayList<?> v = (ArrayList<?>) super.clone();
            v.elementData = Arrays.copyOf(elementData, size);
            v.modCount = 0;
            return v;
        } catch (CloneNotSupportedException e) {
            // this shouldn't happen, since we are Cloneable
            throw new InternalError(e);
        }
    }

无参的toArray浅拷贝自己的内部数据返回
带参的toArray把自己的数据拷贝到入参,并返回。

 public Object[] toArray() {
        return Arrays.copyOf(elementData, size);
    }

 @SuppressWarnings("unchecked")
 public <T> T[] toArray(T[] a) {
        if (a.length < size)
            // Make a new array of a's runtime type, but my contents:
            return (T[]) Arrays.copyOf(elementData, size, a.getClass());
        System.arraycopy(elementData, 0, a, 0, size);
        if (a.length > size)
            a[size] = null;
        return a;
    }

get, set 都会做数组越界验证,set函数,在指定位置设置入参后,返回老数据。

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

        return (E) elementData[index];
    }

    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;
    }

add(E e) ,add(int index, E element)都会发生集合结构的修改,会在ensureCapacityInternal中变更modCount。这个modCount的作用就是确保在修改之中如果别的线程也在修改集合的结构,发现于期望值不符及时抛出异常提示用户。集合的fast-fail机制。add(E e)在尾部追加数据,add(int index, E element) 在指定索引设置,当前位置和后续的顺次后移。ensureCapacityInternal方法确保集合大小能够容纳数据,必要时做扩容处理。

    public boolean add(E e) {
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        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);
        elementData[index] = element;
        size++;
    }

DEFAULTCAPACITY_EMPTY_ELEMENTDATA代表即将进行首次添加数据,且用户未指定0,如果minCapacity小于10,默认扩容至10这个默认值。ensureExplicitCapacity函数在扩容前修改modCount递增1,期望大小minCapacity大于当前容器尺寸调用grow扩容。扩容策略大致是当前大小的1.5倍。( oldCapacity + (oldCapacity >> 1))。minCapacity上溢出则抛异常,否则判断是否在MAX_ARRAY_SIZE内,最多是按Integer.MAX_VALUE设置新的大小。数据的扩容都是新申请一块空间,然后转移数据。扩容代价要比链表高的多。

 private void ensureCapacityInternal(int minCapacity) {
        if (elementData == DEFAULTCAPACITY_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);
    }

  
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

   
    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);
    }

    private static int hugeCapacity(int minCapacity) {
        if (minCapacity < 0) // overflow
            throw new OutOfMemoryError();
        return (minCapacity > MAX_ARRAY_SIZE) ?
            Integer.MAX_VALUE :
            MAX_ARRAY_SIZE;
    }

remove(int index) 首先检查索引有效性,删除属于改变集合结构,所以modCount++,
指定索引位置数据取出,后面的数据前移,最后一个数据位置置空,交给垃圾回收,返回数据。

remove(Object o)遍历集合,找到指定数据位置调用fastRemove。fastRemove的代码与remove中一致。

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; // clear to let GC do its work

        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;
    }


    private void fastRemove(int index) {
        modCount++;
        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        elementData[--size] = null; // clear to let GC do its work
    }

清除所有数据,数据位遍历置空,大小设置0.这里也递增了modCount,因为改动了size.上面的set,get都不改变size。所以不需要改动modCount。

   public void clear() {
        modCount++;

        // clear to let GC do its work
        for (int i = 0; i < size; i++)
            elementData[i] = null;

        size = 0;
    }

排序过程中也不可以并发的改变数据结构,否则会抛出ConcurrentModificationException。

 @Override
    @SuppressWarnings("unchecked")
    public void sort(Comparator<? super E> c) {
        final int expectedModCount = modCount;
        Arrays.sort((E[]) elementData, 0, size, c);
        if (modCount != expectedModCount) {
            throw new ConcurrentModificationException();
        }
        modCount++;
    }

filter是一个删除匹配规则,BitSet是一个存储删除位置的 工具,第一个for循环记录匹配位置,第二个for循环将其余不匹配的依次前移,后面的数据置空,删除完毕。

 @Override
    public boolean removeIf(Predicate<? super E> filter) {
        Objects.requireNonNull(filter);
        // figure out which elements are to be removed
        // any exception thrown from the filter predicate at this stage
        // will leave the collection unmodified
        int removeCount = 0;
        final BitSet removeSet = new BitSet(size);
        final int expectedModCount = modCount;
        final int size = this.size;
        for (int i=0; modCount == expectedModCount && i < size; i++) {
            @SuppressWarnings("unchecked")
            final E element = (E) elementData[i];
            if (filter.test(element)) {
                removeSet.set(i);
                removeCount++;
            }
        }
        if (modCount != expectedModCount) {
            throw new ConcurrentModificationException();
        }

        // shift surviving elements left over the spaces left by removed elements
        final boolean anyToRemove = removeCount > 0;
        if (anyToRemove) {
            final int newSize = size - removeCount;
            for (int i=0, j=0; (i < size) && (j < newSize); i++, j++) {
                i = removeSet.nextClearBit(i);
                elementData[j] = elementData[i];
            }
            for (int k=newSize; k < size; k++) {
                elementData[k] = null;  // Let gc do its work
            }
            this.size = newSize;
            if (modCount != expectedModCount) {
                throw new ConcurrentModificationException();
            }
            modCount++;
        }

        return anyToRemove;
    }

removeAll 是去除ArrayList中的Collection<?> c,留下与之不同的部分
retainAll 是留下ArrayList中的Collection<?> c,留下与之相同的部分

 public boolean removeAll(Collection<?> c) {
        Objects.requireNonNull(c);
        return batchRemove(c, false);
    }

    public boolean retainAll(Collection<?> c) {
        Objects.requireNonNull(c);
        return batchRemove(c, true);
    }

    private boolean batchRemove(Collection<?> c, boolean complement) {
        final Object[] elementData = this.elementData;
        int r = 0, w = 0;
        boolean modified = false;
        try {
            for (; r < size; r++)
                if (c.contains(elementData[r]) == complement)
                    elementData[w++] = elementData[r];
        } finally {
            // Preserve behavioral compatibility with AbstractCollection,
            // even if c.contains() throws.
            if (r != size) {
                System.arraycopy(elementData, r,
                                 elementData, w,
                                 size - r);
                w += size - r;
            }
            if (w != size) {
                // clear to let GC do its work
                for (int i = w; i < size; i++)
                    elementData[i] = null;
                modCount += size - w;
                size = w;
                modified = true;
            }
        }
        return modified;
    }

ArrayList中的迭代器模式,limit是集合的大小,cursor指向下一个。lastRet指向当前待返回。expectedModCount记录当前的集合modCount。
hasNext判断下一个待遍历的是否在limit内。
next函数: 判断是否有并发修改结构发生,是否在limit 内,是否在集合数据长度内,设置cursor: 为下一个待遍历位。lastRet设置为当前返回的位置。
remove: 判断是否有并发修改结构发生,调用集合的remove 方法,更新cursor,limit, expectedModCount。可以看出连续调用两次remove,会因为lastRet < 0抛出异常。

private class Itr implements Iterator<E> {
        protected int limit = ArrayList.this.size;

        int cursor;       // index of next element to return
        int lastRet = -1; // index of last element returned; -1 if no such
        int expectedModCount = modCount;

        public boolean hasNext() {
            return cursor < limit;
        }

        @SuppressWarnings("unchecked")
        public E next() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
            int i = cursor;
            if (i >= limit)
                throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length)
                throw new ConcurrentModificationException();
            cursor = i + 1;
            return (E) elementData[lastRet = i];
        }

        public void remove() {
            if (lastRet < 0)
                throw new IllegalStateException();
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();

            try {
                ArrayList.this.remove(lastRet);
                cursor = lastRet;
                lastRet = -1;
                expectedModCount = modCount;
                limit--;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }

        @Override
        @SuppressWarnings("unchecked")
        public void forEachRemaining(Consumer<? super E> consumer) {
            Objects.requireNonNull(consumer);
            final int size = ArrayList.this.size;
            int i = cursor;
            if (i >= size) {
                return;
            }
            final Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length) {
                throw new ConcurrentModificationException();
            }
            while (i != size && modCount == expectedModCount) {
                consumer.accept((E) elementData[i++]);
            }
            // update once at end of iteration to reduce heap write traffic
            cursor = i;
            lastRet = i - 1;

            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
    }

ArrayList 大致分析结束,并发情况下有可能会抛出ConcurrentModificationException,IndexOutOfBoundsException,NoSuchElementException等异常。在并发环境下使用一般会用Collections.synchronizedList(),内部用同步代码块来包装所有的改变结构的方法。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值