ArrayList(1.8)

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
        
RandomAccess: 实现该接口可以通过下标序号快速访问
Cloneable: 能够被克隆
Serializable:这一个则是支持序列化的
View Code

成员变量

    //这个是表示默认的容量大小是10
    private static final int DEFAULT_CAPACITY = 10;

    //表示这是一个空的数组
    private static final Object[] EMPTY_ELEMENTDATA = {};

   //这个是和上面的那个又区别的,当添加第一个元素的时候会扩容
    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};

   //是真正存储数据的地方
    transient Object[] elementData; // non-private to simplify nested class access

  //这个是数组中元素的个数
    private int size;

构造函数

//这个是通过给定一个具体的值(初始化长度)来进行构造,当然给定的值不能小于0,不然会抛异常
public ArrayList(int initialCapacity) {
        if (initialCapacity > 0) {
            this.elementData = new Object[initialCapacity];
        } else if (initialCapacity == 0) {
            //如果值为0,则会构造一个空的数组
            this.elementData = EMPTY_ELEMENTDATA;
        } else {
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        }
    }
    
    //没有参数传递的话,则默认初始化容量为10,并赋予一个空的数组DEFAULTCAPACITY_EMPTY_ELEMENTDATA
     public ArrayList() {
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }
    
    //使用已有的集合c来创建一个新的ArrayList,
     public ArrayList(Collection<? extends E> c) {
         //先将集合转成数组
        elementData = c.toArray();
        //如果c里面有数据
        if ((size = elementData.length) != 0) {
            // c.toArray might (incorrectly) not return Object[] (see 6260652)
            //判断转换的数组是否是Object[]类型
            if (elementData.getClass() != Object[].class)
                elementData = Arrays.copyOf(elementData, size, Object[].class);
        } else {
            // c里面没有数据
            this.elementData = EMPTY_ELEMENTDATA;
        }
    }

 

修改容量大小

public void trimToSize() {
        modCount++;
        //也就是说数组中实际的个数小于数组的容量
        if (size < elementData.length) {
            elementData = (size == 0)
              ? EMPTY_ELEMENTDATA
              : Arrays.copyOf(elementData, size);
        }
    }
    
    
    //将集合的容量增加为 minCapacity
    public void ensureCapacity(int minCapacity) {
        int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
            // elementData 里面有数据
            ? 0
            // larger than default for default empty table. It's already
            // supposed to be at default size.
            : DEFAULT_CAPACITY;  //值为10

        if (minCapacity > minExpand) {
            ensureExplicitCapacity(minCapacity);
        }
    }
    
    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;

    // *增加容量以确保它至少可以容纳由minimum capacity参数指定的元素数量。
     * @param minCapacity the desired minimum capacity
     */
    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;
    }
    
View Code

 

添加元素

//添加元素到末尾
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++;
    }    
    
 private void rangeCheckForAdd(int index) {
        if (index > size || index < 0)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }
    
    //将集合添加到数组里面
    
   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);

        Object[] a = c.toArray();
        int numNew = a.length;
        ensureCapacityInternal(size + numNew);  // Increments modCount

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

删除元素

//删除指定位置的元素
  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;
    }
    
    
  public boolean remove(Object o) {
          //传过来的值为null
        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;
    }
    
   //这个是把集合清空,也就是将里面的值都值为null
  public void clear() {
        modCount++;

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

        size = 0;
    }
    
    
    //删除fromIndex到toIndex之间的全部元素
    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;
    }
View Code

  其实在查看ArrayList源码的时候你会发现里面其实叶柄灭有你想的那么复杂,在这个数组里面主要就是靠着 System.arraycopy方法来进行操作的。该数组里面默认容量的大小为10

 

posted @ 2018-08-16 17:39  烟雨蒙尘  阅读(148)  评论(0编辑  收藏  举报