ArrayList

  • 是什么,定义?

这是动态的数组,它提供了动态的增加和减少元素,实现了List接口(List实现Collection,所以也实现Collection接口)灵活的设置数组的大小等好处

  • 内部如何实现
1     /**
 2      * The array buffer into which the elements of the ArrayList are stored.
 3      * The capacity of the ArrayList is the length of this array buffer.
 4      */
 5     private transient Object[] elementData;//内部是数组
 6 
 7     /**
 8      * The size of the ArrayList (the number of elements it contains).
 9      *
10      * @serial
11      */
12     private int size;//定义大小
13 
14     /**
15      * Constructs an empty list with the specified initial capacity.
16      *
17      * @param  initialCapacity  the initial capacity of the list
18      * @throws IllegalArgumentException if the specified initial capacity
19      *         is negative
20      */
21     public ArrayList(int initialCapacity) {//带参数的初始化
22         super();
23         if (initialCapacity < 0)
24             throw new IllegalArgumentException("Illegal Capacity: "+
25                                                initialCapacity);
26         this.elementData = new Object[initialCapacity];
27     }
28 
29     /**
30      * Constructs an empty list with an initial capacity of ten.
31      */
32     public ArrayList() {//默认初始化,大小是10
33         this(10);
34     }
  • 添加如何操作
public boolean add(E e) {
         ensureCapacityInternal(size + 1);  // 进行长度的判断与修改(将检查与扩容放在一个方法调用,代码阅读性高)
         elementData[size++] = e;
         return true;
     }
   
     private void ensureCapacityInternal(int minCapacity) {
         modCount++;
         // overflow-conscious code
         if (minCapacity - elementData.length > 0)//进行判断
             grow(minCapacity);
     }
 
     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);
     }
  • 添加重要的一部,扩容,——这是为什么实际应用中要定义数组大小的原因,
//Array的源代码
 public static <T> 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) {//需要复制原来的数组
         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;
     }
  • 制定位置的添加
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 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; // Let gc do its work

        return oldValue;
    }
  • 怎么遍历呢?

数组,通过数组的脚标,遍历;

for each 因为实现Iterator,也可以使用迭代器

使用中改注意:

1、初始化最好定义都用的长度,避免Arrarlist内部复制的操作;

2、如果删除的,尽量从后往前删除

3、不是线程安全,全部代码中都没有synchronized ,

如果有需要,使用Vector,两者的区别3点(其他基本是一样):

线程安全,

多了方法indexOf,

扩容的打大小是:1倍

int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity);