一、继承关系
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
由继承关系知:ArrayList继承自AbstractList类,实现了List和RandomAccess接口,支持克隆,支持序列化
二、基本属性
//默认数组大小为10
private static final int DEFAULT_CAPACITY = 10;
//设置空数组,用于无参构造中初始化数组
private static final Object[] EMPTY_ELEMENTDATA = {};
//ArrayList的底层存储使用的是数组
private transient Object[] elementData;
//集合中存储元素的个数
private int size;
三、构造函数
1.无参构造
public ArrayList() {
super();
//真正初始化数组在add方法中
this.elementData = {} EMPTY_ELEMENTDATA;
}
2.通过传入初始化数组的大小来构造ArrayList
public ArrayList(int initialCapacity) {
super();
//检查参数合法性,若参数为负抛出IllegalArgumentException异常
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
//实例化长度为initialCapacity的数组
this.elementData = new Object[initialCapacity];
}
3.通过传入Collection的集合实例来构造ArrayList
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
size = elementData.length;
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
}
四、常用方法
1.add():添加元素
public boolean add(E e) {
ensureCapacityInternal(size + 1);
//size表示下一个要插入元素的位置
elementData[size++] = e;
return true;
}
//确保容量充足
private void ensureCapacityInternal(int minCapacity) {
if (elementData == EMPTY_ELEMENTDATA) {
//若elementData为空,初始化数组
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
if (minCapacity > elementData.length)
//数组容量不足,执行扩容操作
grow(minCapacity);
}
private void grow(int minCapacity) {
int oldCapacity = elementData.length;
//新数组长度按照原来数组长度的1.5倍扩容
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
elementData = Arrays.copyOf(elementData, newCapacity);
}
2.get():获取元素
public E get(int index) {
rangeCheck(index);
//通过下标位置访问数组元素
return elementData(index);
}
//检查参数合法性
private void rangeCheck(int index) {
if (index < 0 || index >= this.size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
3.remove(Object o):删除元素
public boolean remove(Object o) {
//判断当前对象是否为null对象
if (o == null) {
//循环遍历数组,通过下标删除元素
for (int index = 0; index < size; index++)
//null值情况需要用"=="判断相等
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
//非null值情况需要用".equals()"判断相等
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
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;
}
4.set():修改元素
public E set(int index, E element) {
rangeCheck(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
5.其他方法:
//删除指定集合的元素
public boolean removeAll(Collection<?> c)
//判断集合是否包含元素
public boolean contains(Object o)
//两集合的交集
public boolean retainAll(Collection<?> c)
//将b集合中元素放入a集合中,可数据重复
public boolean addAll(Collection<? extends E> c)
//将集合转化为数组
public Object[] toArray()