public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
private static final long serialVersionUID = 8683452581122892189L;
<span style="color:#006600;"><strong> /**
* 底层实现为Object数组,transient 的意思是在集合序列化时不包含数组对象
*/</strong></span>
private transient Object[] elementData;
/**
*
*<span style="color:#006600;">私有属性,由于现实集合的元素个数</span>
*
*/
private int size;
/**
* <span style="color:#006600;">能够指定集合大小的构造方法</span>
*/
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
/**
* <span style="color:#009900;">默认构造方法</span><span style="color:#006600;">,默认初始化容量为10</span>
*/
public ArrayList() {
this(10);
}
/**
*
*<span style="color:#006600;">第三个构造方法则将提供的集合转成数组返回给elementData(返回若不是Object[]将调用Arrays.copyOf方法将其转为Object[])。</span>
*/
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
size = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
}
/**
* <span style="color:#006600;">如果数组的长度大于集合的大小,则将集合的长度设置为size</span>
*/
public void trimToSize() {
modCount++;
int oldCapacity = elementData.length;
if (size < oldCapacity) {
elementData = Arrays.copyOf(elementData, size);
}
}
/**
*
* <span style="color:#006600;">扩容函数,当想集合中添加元素时会先检查数据容量,如果容量满足则添加元素到集合末尾,如果容量不满足,则进行扩容</span>
*<span style="color:#006600;">因为这个方法扩容之后会将原来的数据进行拷贝,所以或消耗内存,所以在创建集合时,最后能够指定一下集合的容量,这样可以提交集合添加元素时的效率</span>
*
*/
public void ensureCapacity(int minCapacity) {
modCount++;
int oldCapacity = elementData.length;
if (minCapacity > oldCapacity) {
Object oldData[] = elementData;
int newCapacity = (oldCapacity * 3)/2 + 1;
if (newCapacity < minCapacity)
newCapacity = minCapacity;
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
}
/**
*
*<span style="color:#006600;">获取集合中元素的个数</span>
*
*/
public int size() {
return size;
}
/**
*
*<span style="color:#006600;">判断集合中是否为空,即没有一个元素</span>
*
*/
public boolean isEmpty() {
return size == 0;
}
/**
*
*<span style="background-color: rgb(0, 102, 0);"></span><span style="color:#006600;">判断集合中是否含有某个元素</span>
*
*
*/
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
/**
*
*
* <span style="color:#009900;">获取元素在在数组中距开始位置的位置</span>
*
*
*/
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
/**
*
*
* <span style="color:#009900;">获取元素在数组中从末尾开始的所在位置</span>
*
*/
public int lastIndexOf(Object o) {
if (o == null) {
for (int i = size-1; i >= 0; i--)
if (elementData[i]==null)
return i;
} else {
for (int i = size-1; i >= 0; i--)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
/**
*
* <span style="color:#006600;">克隆方法</span>
*
*
*/
public Object clone() {
try {
ArrayList<E> v = (ArrayList<E>) 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();
}
}
/**
*
*
*
* <span style="color:#006600;">将集合转为数组</span>
*
*/
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
/**
*
*
*
*
* <span style="color:#006600;">将集合转为指定类型的数组</span>
*
*
*/
public <T> T[] toArray(T[] a) {
if (a.length < size)
// Make a new array of a's runtime type, but my contents:
return (T[]) Arrays.copyOf(elementData, size, a.getClass());
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
// Positional Access Operations
/**
*
*<span style="color:#006600;">获取集合中指定位置的元素</span>
*/
public E get(int index) {
RangeCheck(index);
return (E) elementData[index];
}
/**
*
*
*
*
* <span style="color:#006600;">将集合中指定位置替换为另一元素并返回替换前的元素</span>
*
*
*/
public E set(int index, E element) {
RangeCheck(index);
E oldValue = (E) elementData[index];
elementData[index] = element;
return oldValue;
}
/**
*
*
* <span style="color:#006600;">添加元素添加之前先进行扩容检查</span>
*
*/
public boolean add(E e) {
ensureCapacity(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
/**
*
* <span style="color:#006600;">在指定位置添加元素</span>,<span style="color:#006600;">此方法可能会影响程序的性能,因为添加的元素如果在数组的靠前位置,那么数组的重组的开销就会很大</span>
*
*
*/
public void add(int index, E element) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);
ensureCapacity(size+1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
/**
*
*
*
*
* <span style="color:#006600;">删除指定位置的元素,同样需要数组重组操作,如果删除的元素特别靠前,那么数组重组的消耗依然很大</span>
*
*
*/
public E remove(int index) {
RangeCheck(index);
modCount++;
E oldValue = (E) elementData[index];
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // Let gc do its work
return oldValue;
}
/**
*
* <span style="color:#006600;">删除指定元素</span>
*
*
*/
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;
}
/*
*<span style="color:#006600;">删除元素</span>
*
*/
private void fastRemove(int index) {
modCount++;
int numMoved = size - index - 1;//<span style="color:#006600;">要移动的元素个数</span>
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);//<span style="color:#006600;">数组重组</span>
elementData[--size] = null; // <span style="color:#006600;">将重组之后的数组的最后位置的元素赋值为null</span>
}
/**
*
* <span style="color:#006600;">清空集合,实际就是变了数组,将数组的各个位置赋值为Null</span>,<span style="color:#006600;">同时将size赋值为0</span>
*/
public void clear() {
modCount++;
// Let gc do its work
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
/**
*
*
*
*
*<span style="color:#006600;"> 添加一个集合</span>
*
*
*
*/
public boolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray();//<span style="color:#006600;">将集合转为数组</span>
int numNew = a.length;//<span style="color:#006600;">取得数组的长度</span>
ensureCapacity(size + numNew); // <span style="color:#006600;">扩容</span>
System.arraycopy(a, 0, elementData, size, numNew);//<span style="color:#006600;">将数组添加到elementData数组的末尾</span>
size += numNew;
return numNew != 0;
}
/**
*
*
*
*
*<span style="color:#006600;"> 添加一个集合到指定位置</span>
*
*
*/
public boolean addAll(int index, Collection<? extends E> c) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(
"Index: " + index + ", Size: " + size);
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacity(size + numNew); // Increments modCount
int numMoved = size - index;
if (numMoved > 0)
System.arraycopy(elementData, index, elementData, index + numNew,
numMoved);
System.arraycopy(a, 0, elementData, index, numNew);
size += numNew;
return numNew != 0;
}
/**
*
*
*
* <span style="color:#006600;">删除指定范围的元素</span>
*
*
*/
protected void removeRange(int fromIndex, int toIndex) {
modCount++;
int numMoved = size - toIndex;//<span style="color:#006600;">要移动的元素个数</span>
System.arraycopy(elementData, toIndex, elementData, fromIndex,
numMoved);
// Let gc do its work
int newSize = size - (toIndex-fromIndex);
while (size != newSize)
elementData[--size] = null;
}
/**
*
*
* <span style="color:#FFFFFF;"><span style="background-color: rgb(0, 102, 0);">下标越界检查</span></span>
*
*/
private void RangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);
}
/**
*
*
*
*<span style="color:#006600;"> 序列化对象</span>
*
*
*/
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 array length
s.writeInt(elementData.length);
// 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();
}
}
/**
*
* <span style="color:#006600;">反序列化</span>
*/
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in size, and any hidden stuff
s.defaultReadObject();
// Read in array length and allocate array
int arrayLength = s.readInt();
Object[] a = elementData = new Object[arrayLength];
// Read in all elements in the proper order.
for (int i=0; i<size; i++)
a[i] = s.readObject();
}
}