ArrayList实现源码分析

本文将以如下几个问题来探讨ArrayList的源码实现
1.ArrayList的大小是如何自动增长的
2.什么状况下你会使用ArrayList?何时你会选择LinkedList?
3.如何复制某个ArrayList到另外一个ArrayList中去?写出你的代码?
4.在索引中ArrayList的增长或者删除某个对象的运行过程?效率很低吗?解释一下为何?
5.Interator在ArrayList的实现html

关于Java集合的小抄 关于ArrayList的描述: *以数组实现。节约空间,但数组有容量限制。超出限制时会增长50%容量,用System.arraycopy()复制到新的数组,所以最好能给出数组大小的预估值。默认第一次插入元素时建立大小为10的数组。 按数组下标访问元素--get(i)/set(i,e) 的性能很高,这是数组的基本优点。 直接在数组末尾加入元素--add(e)的性能也高,但若是按下标插入、删除元素--add(i,e), remove(i), remove(e),则要用System.arraycopy()来移动部分受影响的元素,性能就变差了,这是基本劣势。* java

一、ArrayList的大小是如何自动增长的数组

直接上代码吧,每次add的时候都会判断是否须要扩容,如下是扩容的主要方法 ```java 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); } 数据结构

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

private void grow(int minCapacity) {
    // overflow-conscious code
    int oldCapacity = elementData.length;
    //左移1位,至关于除以2,就是容量提升50%
    int newCapacity = oldCapacity + (oldCapacity >> 1);
    if (newCapacity - minCapacity < 0)
        newCapacity = minCapacity;
    //最大的阀值MAX_ARRAY_SIZE
    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?何时你会选择LinkedList?**
<p>咱们知道ArrayList和LinkedList的数据结构是不一样的,ArrayList是以连续的数组进行存储的,因此它的get是常数级别的,LinkedList是双向链表存储的。他的查询最坏状况是n。觉得ArrayList是数组存储的,因此当你查找某一指定索引的数据时,它每次删除和指定索引添加都要移动数组的位置,其内部的实现方式是数组复杂用到System.arraycope,是比较影响性能的,而双向链表删除和插入只要找到相应的节点位置,关联下指针,因此性能会更好。
```java
    public void add(int index, E element) {
        //判断是否超出了索引
        rangeCheckForAdd(index);
        //判断是否须要扩容
        ensureCapacityInternal(size + 1);  // Increments modCount!!
        //讲elementData复制到elementData,从index开始复制,从index+1开始粘贴,复制的长度是size-index
        System.arraycopy(elementData, index, elementData, index + 1,
                         size - index);
        elementData[index] = element;
        size++;
    }

以及删除方法app

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

三、如何复制某个ArrayList到另外一个ArrayList中去?写出你的代码?ide

下面就是把某个ArrayList复制到另外一个ArrayList中去的几种技术: 性能

使用clone()方法,好比ArrayList newArray = oldArray.clone() ui

使用ArrayList构造方法,好比:ArrayList myObject = new ArrayList(myTempObject) this

其余 ```java 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); } } ``` ```java public ArrayList(Collection c) { elementData = c.toArray(); size = elementData.length; // c.toArray might (incorrectly) not return Object[] (see 6260652) if (elementData.getClass() != Object[].class) elementData = Arrays.copyOf(elementData, size, Object[].class); } ``` 指针

综上所述,最终的复制方式都是调用Arrays.copyOf,而Arrays.copyOf是调用System.arrayscopy ```java public static T[] copyOf(T[] original, int newLength) { return (T[]) copyOf(original, newLength, original.getClass()); }

public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
    @SuppressWarnings("unchecked")
    T[] copy = ((Object)newType == (Object)Object[].class)
        ? (T[]) new Object[newLength]
        : (T[]) Array.newInstance(newType.getComponentType(), newLength);
    System.arraycopy(original, 0, copy, 0,
                     Math.min(original.length, newLength));
    return copy;
}
**四、在索引中ArrayList的增长或者删除某个对象的运行过程?效率很低吗?**
<p>这个问题同2,由于添加删除某个索引的数据时,须要总体移动数组,因此效率比较低。
**五、Interator在ArrayList的实现**
<p>由于这个实现的代码比较简单这里就很少解释了,特别说明下forEachRemaining,这个方法是jdk1.8加上的,支持lamdba表达式,主要是遍历游标后面的数据,看while循环i++
```java
    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;
        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();
            //游标的位置加1
            cursor = i + 1;
            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();
        }
    }

参考

相关文章
相关标签/搜索