package java.util;
一。public class ArrayList<E> extends AbstractList<E>implements List<E>, RandomAccess, Cloneable, java.io.Serializable
extends继承抽象类AbstractList<E> implements实现了...Serializable序列化。存储到内存中
二。private static final long serialVersionUID = 8683452581122892189L;标识序列化的版本id(兼容性问题)
三。 public ArrayList() {
this(10); //无参构造方法 默认10个长度new 对象就是10个
}
四。 public void trimToSize() { //删除预留的ArrayList空间
modCount++;
int oldCapacity = elementData.length;
if (size < oldCapacity) {
elementData = Arrays.copyOf(elementData, size);
}
}
五。 public void ensureCapacity(int minCapacity) {//预先设置参数list大小.加快初始化速度,如add()之前
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);
}
}
六。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();
}
}
七。 public <T> T[] toArray(T[] a) {//类型转换,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;
}
public Object[] toArray() {//2个多态方法。
return Arrays.copyOf(elementData, size);
}
//不带参数的toArray方法,是构造的一个Object数组,然后进行数据拷贝,此时进行转型就会产生ClassCastException,使用T[]a使转型类型一致
八。 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++;
}
九。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 移除的数组下标设置为Null
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; // Let gc do its work 通过设置数组下标的值为NULL
}
十。public void clear() {//清空数组
modCount++;
// Let gc do its work
for (int i = 0; i < size; i++)
elementData[i] = null; //把每个数组值都设为NULL
size = 0; //长度设为0
}
十一。public boolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacity(size + numNew); // Increments modCount 加快初始化速度
System.arraycopy(a, 0, elementData, size, numNew);
size += numNew;
return numNew != 0;
}
十二。protected void removeRange(int fromIndex, int toIndex) {//删除指定下标之间的数组值
modCount++;
int numMoved = size - toIndex;
System.arraycopy(elementData, toIndex, elementData, fromIndex,
numMoved);
// Let gc do its work
int newSize = size - (toIndex-fromIndex);
while (size != newSize)
elementData[--size] = null;
}
十三。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();
}
}
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();
}
}