ArrayList
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
**RandomAccess:**实现了该接口表明支持快速随机访问,访问时间复杂度为O(1);
**Cloneable: **ArrayList重写了clone方法来实现对象的复制;
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);
}
}
**Serializable:**支持序列化;
ArrayList的成员变量
**serialVersionUID:**序列化版本号,用来对序列化进行校验,保证序列化的安全性;
private static final long serialVersionUID = 8683452581122892189L;
**DEFAULT_CAPACITY:**对象数组初始化容量大小;
private static final int DEFAULT_CAPACITY = 10;
**EMPTY_ ELEMENTDATA:**表示初始容量为0的ArrayList的默认空数组,添加第一个元素时,将数组长度扩大为默认值(默认值为10);
private static final Object[] EMPTY_ELEMENTDATA = {};
**DEFAULTCAPACITY _EMPTY_ELEMENTDATA:**表示一个空的ArrayList对象,即ArrayList中没有任何元素,添加元素时,将数组长度扩大为1,然后将元素添加到数组中。
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
**elementData:**对象数组
transient Object[] elementData;
size: ArrayList中元素个数
private int size;
三种构造方法
1.传入初始化容量
2.无参构造
3.传入集合
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
if ((size = elementData.length) != 0) {
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
} else {
// replace with empty array.
this.elementData = EMPTY_ELEMENTDATA;
}
}
扩容机制
trimToSize 调整ArrayList中对象数组的大小,调整为列表的当前大小,节省内存
public void trimToSize() {
// modCount记录了ArrayList结构被修改的次数,在迭代过程中如果发生了变化,可能会引发异常。
modCount++;
if (size < elementData.length) {
elementData = (size == 0)
? EMPTY_ELEMENTDATA
: Arrays.copyOf(elementData, size);
}
}
add()
public boolean add(E e) {
// 会去判断是否需要扩容
ensureCapacityInternal(size + 1); // Increments modCount!!
// 其实就是一个数组赋值的过程
elementData[size++] = e;
return true;
}
ensureCapacityInternal()
private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
calculateCapacity()
计算加入元素后列表的大小,如果是空数组,再添加完元素后会直接初始化为10
private static int calculateCapacity(Object[] elementData, int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity;
}
ensureExplicitCapacity()
判断是否需要扩容
private void ensureExplicitCapacity(int minCapacity) {
//修改结构后记录+1
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
grow()
private void grow(int minCapacity) {
// overflow-conscious code
// 计算旧的容量,通过位运算进行1.5倍扩容
// 为什么1.5? 性能和内存的平衡 经验
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);
}
hugeCapacity()
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}