public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable ArrayList继承了AbstractList抽象类,实现了List、RandomAccess、Cloneable、Serializable接口。
ArrayList的默认初始容量为10,阅读源码可知--底层数据结构是数组。添加的元素是可以重复的。
底层数据结构是数组,查询快,增加、删除元素慢(因为每增加或删除元素,数组的其余位置的元素都需要进行变动)。
/**
* Default initial capacity.
* 默认初始容量为10
*/
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; // 非私有,以简化嵌套类访问。被transient修饰的,不能被序列化 /**
* The size of the ArrayList (the number of elements it contains).
* ArrayList的长度(元素个数)
* @serial
*/
private int size;
ArrayList的构造方法的源码:
/**
* 使用默认容量构造一个数组
*
* @param initialCapacity 这个列表的初始化容量
* @throws IllegalArgumentException 如果初始化容量initialCapacity是一个负数,则会抛出这个IllegalArgumentException异常
*/
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);
}
} /**
* Constructs an empty list with an initial capacity of ten.
* 构造初始容量为10的空数组
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
} /**
* 构造包含指定的元素的列表集合,按照顺序返回这个集合的迭代器。
*
* @param c 要放到集合中的Collection
* @throws NullPointerException 如果传入的Collection为null,则会抛出NullPointerException空指针异常
*/
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;
}
}
重写了clone方法。
/**
* Returns a shallow copy of this <tt>ArrayList</tt> instance. (The elements themselves are not copied.)
* 返回此ArrayList实例的浅拷贝。(元素本身不会被复制。)
* @return a clone of this <tt>ArrayList</tt> instance
*/
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);
}
}
add方法:
/**
* 将指定元素追加到列表末尾。所以ArrayList是有序的,也就是元素的存储顺序和添加顺序是一致的。
*
* @param e element to be appended to this list 追加到列表中的元素
* @return <tt>true</tt> (as specified by {@link Collection#add})
*/
public boolean add(E e) {
//确保内部的容量
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;//在list末尾追加元素
return true;
} /**
* 在list的指定索引处插入指定元素
* Inserts the specified element at the specified position in this
* list. Shifts the element currently at that position (if any) and
* any subsequent elements to the right (adds one to their indices).
*
* @param index 被插入的索引位置 index at which the specified element is to be inserted
* @param element 被插入的元素 element to be inserted
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
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++;
}
remove方法:
/**
* 移除指定位置的元素,将指定位置元素之后的元素左移动。
* Removes the element at the specified position in this list.
* Shifts any subsequent elements to the left (subtracts one from their
* indices).
*
* @param index 要被移除的元素的索引 the index of the element to be removed
* @return 返回ArrayList中被移除的元素 the element that was removed from the list
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E remove(int index) {
//检查索引是否越界,如果越界,则抛出IndexOutOfBoundsException异常
rangeCheck(index); modCount++;
E oldValue = elementData(index);//取出要被移除的元素 //将被删除的元素的索引位置右边的所有元素往左移动一位
int numMoved = size - index - 1;
if (numMoved > 0) {
System.arraycopy(elementData, index + 1, elementData, index,
numMoved);
}
//将ArrayList中原先最后位置的索引位置置为null,以便GC完成它的工作
elementData[--size] = null; // clear to let GC do its work return oldValue;//返回被移除的元素
} /**
* Removes the first occurrence of the specified element from this list,
* if it is present. If the list does not contain the element, it is
* unchanged. More formally, removes the element with the lowest index
* <tt>i</tt> such that
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>
* (if such an element exists). Returns <tt>true</tt> if this list
* contained the specified element (or equivalently, if this list
* changed as a result of the call).
*
* @param o element to be removed from this list, if present
* @return <tt>true</tt> if this list contained the specified element
*/
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;
}
/**
* 增加容量,以确保它至少可以持有由最小容量参数指定的元素容量。
* Increases the capacity to ensure that it can hold at least the
* number of elements specified by the minimum capacity argument.
*
* @param 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);//
}
/**
* 返回ArrayList中指定位置(索引index处)的元素
* Returns the element at the specified position in this list.
*
* @param index 索引 index of the element to return
* @return list中指定索引处的元素 the element at the specified position in this list
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E get(int index) {
//检查索引是否越界
rangeCheck(index);
//返回指定索引处index的元素
return elementData(index);
}
/**
* 将ArrayList中某个索引index位置上的元素A替换成你指定的元素B
* Replaces the element at the specified position in this list with
* the specified element.
*
*
* @param index 替换的元素的索引 index of the element to replace
* @param element 替换后的元素 element to be stored at the specified position
* @return 该位置上替换前的元素 the element previously at the specified position
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E set(int index, E element) {
//检查索引是否越界
rangeCheck(index);
//获取index索引处原先的元素
E oldValue = elementData(index);
//将index索引处的元素替换成你指定的元素element
elementData[index] = element;
//返回index索引处原先的元素
return oldValue;
}
/**
* A version of rangeCheck used by add and addAll.
* 进行范围检查,也就是索引是否越界
*/
private void rangeCheckForAdd(int index) {
//如果索引越界,则抛出IndexOutOfBoundsException异常
if (index > size || index < 0) {
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
} /**
* 构造IndexOutOfBoundsException异常的详细信息
* Constructs an IndexOutOfBoundsException detail message.
* Of the many possible refactorings of the error handling code,
* this "outlining" performs best with both server and client VMs.
*/
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size;
}