ArrayList源码解析

之前看过ArrayList(线程不安全)源码(jdk l.8),但没有去记录它,今天想了一下还是应该记录一下。与Vector(线程安全,方法被synchronized修饰)相比,除了扩容机制不太一样,Verctor可以指定扩容大小,默认扩成原来2倍,ArrayList是扩成1.5倍(扩容后可以装下元素的情况下)。
ArrayList定义:

从类定义来看:支持泛型,有序集合,可随机访问,可克隆,可序列化
public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable

数据成员

数据成员:
//默认容量为10
private static final int DEFAULT_CAPACITY = 10;
//没有存放任何类型的Object[],表示空数组,除了在无参构造器容器元素个数为空都用这个表示
private static final Object[] EMPTY_ELEMENTDATA = {};
//默认没有存放任何类型的Object[],在无参构造器赋值给elementData
 private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
 //存放元素的Object[],ArrayList底层使用这个数组存放元素的,有动态数组之称,感觉
 和StringBuffer 差不多,StringBuffer底层存放元素的是char[]
 transient Object[] elementData;
 //数组元素个数
 private int size;
 //集合最大存储元素个数
  private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

构造器

//根据指定长度初始化
 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);
        }
    }
	//默认初始化
	public ArrayList() {
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }
	//根据集合初始化,若集合c为空,elementData = EMPTY_ELEMENTDATA
	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;
        }
    }

先介绍最主要(扩容机制)的添加元素方法add(),以及扩容,和容量有关的方法,add()不做详细介绍,因为操作数组大家都比较熟悉。
add():

//把元素插入到尾部
	public boolean add(E e) {
		//判断是否要扩容
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        elementData[size++] = e;
        return true;
    }
    //根据下标添加元素
	public void add(int index, E element) {
        rangeCheckForAdd(index);

        ensureCapacityInternal(size + 1);  // Increments modCount!!
        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);  // Increments modCount
        System.arraycopy(a, 0, elementData, size, numNew);
        size += numNew;
        return numNew != 0;
    }
    //根据下标将集合添加到数组中
	public boolean addAll(int index, Collection<? extends E> c) {
            rangeCheckForAdd(index);
            int cSize = c.size();
            if (cSize==0)
                return false;

            checkForComodification();
            parent.addAll(parentOffset + index, c);
            this.modCount = parent.modCount;
            this.size += cSize;
            return true;
        }
	//保证元素添加有序
	private void rangeCheckForAdd(int index) {
        if (index > size || index < 0)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

下面介绍的是与扩容相关的方法:

//添加元素之前会调用这个方法,判断是否需要扩容
private void ensureCapacityInternal(int minCapacity) {
        ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
    }
    //计算出要真正最小扩容的容量
	private static int calculateCapacity(Object[] elementData, int minCapacity) {
		//默认构造器首次添加元素会扩容,且容量为DEFAULT_CAPACITY 10
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            return Math.max(DEFAULT_CAPACITY, minCapacity);
        }
		//否则返回size+1
        return minCapacity;
        //明确扩容容量
	 private void ensureExplicitCapacity(int minCapacity) {
        modCount++;
        // overflow-conscious code
		//假如size+1大于容器容量,则扩容,默认构造器首次扩容为10
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }
    //这个是真正扩容的方法
	private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
		//这里是ArrayList扩容方式,扩为原来的1.5倍
        int newCapacity = oldCapacity + (oldCapacity >> 1);
		//指定<4容量的容器(此时newCapacity = minCapacity),或一次性添加多个元
		//素(minCapacity>=newCapacity),例如addAll方法会满足这个要求,下次添加元素就要扩容
        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);
    }
	
    }

与容量有关的方法:

//容器调整为size长度(size=0,elementData=EMPTY_ELEMENTDATA),若这个容器需要添加元素就需要
扩容了
	public void trimToSize() {
        modCount++;
        if (size < elementData.length) {
            elementData = (size == 0)
              ? EMPTY_ELEMENTDATA
              : Arrays.copyOf(elementData, size);
        }
    }
    //这个方法只能增大容量不能减少容量,(如果是默认构造器,以10为容量大小参与比较),指定扩大
    成minCapacity
	public void ensureCapacity(int minCapacity) {
		//只有无参构造器会为true
        int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
            // any size if not default element table
            ? 0
            // larger than default for default empty table. It's already
            // supposed to be at default size.
            : DEFAULT_CAPACITY;

        if (minCapacity > minExpand) {
            ensureExplicitCapacity(minCapacity);
        }
    }

set和get:

//返回旧值
	 public E set(int index, E element) {
        rangeCheck(index);

        E oldValue = elementData(index);
        elementData[index] = element;
        return oldValue;
    }
	public E get(int index) {
        rangeCheck(index);

        return elementData(index);
    }
	//这个是保证下标元素不为空
	 private void rangeCheck(int index) {
        if (index >= size)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

remove:

	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);
          //将原先最后元素位置设置为null,size减一                   
        elementData[--size] = null; // clear to let GC do its work

        return oldValue;
    }
    //个人是觉得null不能用equals方法比较
	 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;
    }
    //和remove(int index)差不多
	 protected void removeRange(int fromIndex, int toIndex) {
        modCount++;
        int numMoved = size - toIndex;
        System.arraycopy(elementData, toIndex, elementData, fromIndex,
                         numMoved);

        // clear to let GC do its work
        int newSize = size - (toIndex-fromIndex);
        for (int i = newSize; i < size; i++) {
            elementData[i] = null;
        }
        size = newSize;
    }

clone:

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

其他方法,都比较简单,就直接放一起了

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;
    }
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;
    }
     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 boolean contains(Object o) {
        return indexOf(o) >= 0;
    }
     public boolean isEmpty() {
        return size == 0;
    }
    public int size() {
        return size;
    }
public void clear() {
        modCount++;

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

        size = 0;
    }

迭代器方法的实现

//list特有
public ListIterator<E> listIterator() {
        return new ListItr(0);
    }
	public Iterator<E> iterator() {
        return new Itr();
    }
	
	 private class Itr implements Iterator<E> {
	//指向下次访问元素的下标
        int cursor;       // index of next element to return
        //上次下标位置
        int lastRet = -1; // index of last element returned; -1 if no such
        int expectedModCount = modCount;

        Itr() {}

        public boolean hasNext() {
            return cursor != size;
        }

        @SuppressWarnings("unchecked")
        public E next() {
            checkForComodification();
            int i = cursor;
            if (i >= size)
                throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length)
                throw new ConcurrentModificationException();
            cursor = i + 1;
            //返回获取到的元素,并用lastRet保存下标位置
            return (E) elementData[lastRet = i];
        }

        public void remove() {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();

            try {
                ArrayList.this.remove(lastRet);
                cursor = lastRet;
                //因为只保存了上次下标,所以不能连续删除,只能删除一次
                lastRet = -1;
                expectedModCount = modCount;
            } 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;
            checkForComodification();
        }

        final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
    }
    //可以双向遍历,因为底层是数组,可以随机访问元素,还可以使用add添加元素,和set修改元素,所以
    使用这个迭代器遍历时可以添加修改元素而不引发fast-fail机制
	 private class ListItr extends Itr implements ListIterator<E> {
        ListItr(int index) {
            super();
            cursor = index;
        }

        public boolean hasPrevious() {
            return cursor != 0;
        }

        public int nextIndex() {
            return cursor;
        }

        public int previousIndex() {
            return cursor - 1;
        }

        @SuppressWarnings("unchecked")
        public E previous() {
            checkForComodification();
            int i = cursor - 1;
            if (i < 0)
                throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length)
                throw new ConcurrentModificationException();
            cursor = i;
            return (E) elementData[lastRet = i];
        }
	//根据分析,remove之后不能set,set之后也不能remove,因为remove之后lastRet =-1
        public void set(E e) {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();

            try {
                ArrayList.this.set(lastRet, e);
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }
		//根据分析,add之后不能remove,因为add之后lastRet =-1,这个add与ArrayList类中add
		不一样,它不是在尾部插入元素,而是插入到访问元素的后面。
        public void add(E e) {
            checkForComodification();

            try {
                int i = cursor;
                ArrayList.this.add(i, e);
                cursor = i + 1;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值