最近看了一下List集合的源码,简单记录一下
- 创建ArrayList:
/**
* Default initial capacity.
*/
private static final int DEFAULT_CAPACITY = 10;
/**
* Shared empty array instance used for empty instances.
*/
private static final Object[] EMPTY_ELEMENTDATA = {};
/**
* Shared empty array instance used for default sized empty instances. We
* distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
* first element is added.
*/
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
/**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer. Any
* empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA
* will be expanded to DEFAULT_CAPACITY when the first element is added.
*/
transient Object[] elementData; // non-private to simplify nested class access
/**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
private int size;
ArrayList里有默认容量,默认数组这些基本属性
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
- list.add(E)
如果创建ArrayList的时候不带参数,则创建一个默认size是0的空数组,并在第一次调用add()方法的时候进行扩容,将容量设置为10
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
后面添加数据的时候,会判断当前数组的length和所要添加后数据后容量的大小
if (minCapacity - elementData.length > 0)
grow(minCapacity);
如果大于数组的length,则需要进行扩充
int newCapacity = oldCapacity + (oldCapacity >> 1);
根据源码可以看到,每次扩充是增加现在大小的一般,比如现在容量是10,当第十一给数据进来的时候,容量会增加到15
elementData = Arrays.copyOf(elementData, newCapacity);
扩容完毕后,将之前的数组,复制到新的容量的数组中去
elementData[size++] = e;
然后将当前要add的值,放在数组的这个下标上,注意这里是size++,默认是0
- list.add(index, E)
和只传E的方法差不多
private void rangeCheckForAdd(int index) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
每次进来先会判断下标是否越界
然后判断释放需要进行扩容,和上面一样
插入数据这块不一样,如下
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
是把以前的数组,从index到末尾,向后移动一个下标,然后将当前要add的元素,插入在index位置
- remove(E)方法
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;
}
}
先判断要删除的值是否是null,然后逐个遍历,找到第一个值进行删除
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
具体操作是将要删除的这个元素的下标的后一位一直到末尾,向前挪动一个位置,然后将数组的最后一位置为null,方便GC清理
每次用remove(E)方法只能删除一个值,假如你list中存了3个张三,你remove(“张三”),只能删掉一个,还有两个,如果想要全部删掉还得用removeAll(Collection<?> c)
- removeAll(Collection<?> c)
for (; r < size; r++)
if (c.contains(elementData[r]) == complement)
elementData[w++] = elementData[r];
complement为false,遍历集合,挑选出不是张三的元素,放在一个新的数组中,然后将这个新的数组复制到ArrayList的数组中,并删掉后续的引用,方便GC,如下
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;
}