ArrayList
是一個有序列表。
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
-
RandomAccess
標識接口,無內部方法,表示可以隨機訪問內部元素 -
Cloneable
標識接口,無內部方法,表示可以進行負責 -
java.io.Serializable
標識接口,無內部方法,表示支持序列化
底層數據結構是一個對象數組
transient Object[] elementData;
ArrayList對象有存儲上限
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
縮容,當buffer分配的空間容量大于實際存儲的元素數量時,通過縮容釋放對于的資源。
public void trimToSize() {
modCount++;
if (size < elementData.length) {
elementData = (size == 0)
? EMPTY_ELEMENTDATA
: Arrays.copyOf(elementData, size);
}
}
擴容,步長為原容量的 50%, 擴容后將原buffer復制到新創建的buffer中。所以ArrayList如果存在頻繁擴容的情況,會引起程序性能下降。
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);
}
插入或刪除指定位置的Element,都會引起底層數據的批量復制
插入
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; // clear to let GC do its work
return oldValue;
}
private void fastRemove(int index) {
modCount++;
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時,直接將對象內存釋放。
public void clear() {
modCount++;
// clear to let GC do its work
// 釋放句柄,告訴GC對象可以被回收,但并不是立即回收
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
在數據計算中存在集合求交集運算,在ArrayList中同樣提供了該方法。
求交集
public boolean retainAll(Collection<?> c) {
Objects.requireNonNull(c);
return batchRemove(c, true);
}
private boolean batchRemove(Collection<?> c, boolean complement) {
final Object[] elementData = this.elementData;
int r = 0, w = 0;
boolean modified = false;
try {
for (; r < size; r++)
if (c.contains(elementData[r]) == complement)
elementData[w++] = elementData[r];
} finally {
// Preserve behavioral compatibility with AbstractCollection,
// even if c.contains() throws.
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;
}
}
return modified;
}
ArrayList實現了Serializable接口,表示它可以被序列化,在其內部提供了jdk標準的序列化和反序列號的方法。通過序列化和發序列化方法可以發現,JDK并不是把ArrayList所有屬性都寫入文件,只是將size,以及數組中的每一個對象進行了序列化。
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException{
// Write out element count, and any hidden stuff
int expectedModCount = modCount;
s.defaultWriteObject();
// Write out size as capacity for behavioural compatibility with clone()
s.writeInt(size);
// Write out all elements in the proper order.
for (int i=0; i<size; i++) {
s.writeObject(elementData[i]);
}
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
elementData = EMPTY_ELEMENTDATA;
// Read in size, and any hidden stuff
s.defaultReadObject();
// Read in capacity
s.readInt(); // ignored
if (size > 0) {
// be like clone(), allocate array based upon size not capacity
ensureCapacityInternal(size);
Object[] a = elementData;
// Read in all elements in the proper order.
for (int i=0; i<size; i++) {
a[i] = s.readObject();
}
}
}