ArrayList 继承 AbstractList<E> 实现:List<E>, RandomAccess, Cloneable, java.io.Serializable
属性:
private static final int DEFAULT_CAPACITY = 10; //初始化一个容量大小
private static final Object[] EMPTY_ELEMENTDATA = {};//这是一个空的数组
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};//这是一个默认大小的空数组
transient Object[] elementData; //数组引用
构造函数
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity]; //创建一个数组,数组长度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) { //传入的参数是Collection
elementData = c.toArray(); //将c转换为数组类型传入
if ((size = elementData.length) != 0) {
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
} else {
this.elementData = EMPTY_ELEMENTDATA;
}
}
方法:
public void ensureCapacity(int minCapacity) { //指定最小参数 如果数组的初始化没有设定初始值,
int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA) //那么数组就是一个空数组,数组长度为0
// any size if not default element table
? 0
: DEFAULT_CAPACITY; //minExpand为0或者DEFAULT_CAPACITY
if (minCapacity > minExpand) {
ensureExplicitCapacity(minCapacity);
}
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0) //判断最小长度大于elementData.length
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);
}
private static int hugeCapacity(int minCapacity) { //判断越界问题
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ? //获取最大长度
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
public int size() {
return size; //获取数组长度
}
public boolean isEmpty() {
return size == 0; //判断是否为空
}
public boolean contains(Object o) {
return indexOf(o) >= 0; //判断是否包含o
}
public int indexOf(Object o) {
if (o == null) { //判断是否存在o
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i; //如果不存在o则返回数组的长度
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i])) //判断是否包含o
return i; //返回
}
return -1;
}
public <T> T[] toArray(T[] a) {
if (a.length < size)
return (T[]) Arrays.copyOf(elementData, size, a.getClass()); //拷贝一个数组,扩展数组长度为size
System.arraycopy(elementData, 0, a, 0, size); //创建一个数组,把elementData从开始位置复制到 a数组从0位置开始,长度为size
if (a.length > size)
a[size] = null;
return a;
}
E elementData(int index) {
return (E) elementData[index]; // 返回一个对象
}
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); //判断是否越界
}
public E get(int index) {
rangeCheck(index);
return elementData(index); //获取指定位置的对象
}
public E set(int index, E element) {
rangeCheck(index);
E oldValue = elementData(index); //获取旧的对象
elementData[index] = element; // 在该位置插入新的值
return oldValue;
}
private void ensureCapacityInternal(int minCapacity) { //minCapacity=size+1
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); //判断默认值和最小长度的值谁最大
}
ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(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); //1.5oldCapacity
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);
}
private static int hugeCapacity(int minCapacity) { //判断越界问题
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ? //获取最大长度
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e; //添加
return true;
}
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;
}
public boolean remove(Object o) {
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;
}
}
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; // clear to let GC do its work
}
public void clear() {
modCount++;
// clear to let GC do its work
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}