interface Iterable : //實(shí)現(xiàn)此接口使集合對(duì)象可以通過(guò)迭代器遍歷自身元素
? Iterator iterator();
public interface Collection extends Iterable: //接口繼承接口使用:extends
int size();
boolean isEmpty();
boolean contains(Object o);
boolean containsAll(Collection c);
boolean add(E e);
boolean addAll(Collection c);
boolean remove(Object o);
boolean removeAll(Collection c);
void clear();
boolean equals(Object o);
int hashCode();
boolean retainAll(Collection c);
Iterator iterator();
Object[] toArray();
T[] toArray(T[] a);
public interface List extends Collection
int size();
boolean isEmpty();
boolean contains(Object o);
Iterator iterator();
Object[] toArray();
T[] toArray(T[] a);
boolean add(E e);
boolean remove(Object o);
boolean removeAll(Collection c);
boolean containsAll(Collection c);
boolean addAll(Collection c);
boolean addAll(int index, Collection c);
boolean retainAll(Collection c);
void clear();
boolean equals(Object o);
int hashCode();
E get(int index);
E set(int index, E element);
void add(int index, E element);
E remove(int index);
int indexOf(Object o);
int lastIndexOf(Object o);
ListIterator listIterator();
ListIterator listIterator(int index);
List subList(int fromIndex, int toIndex);
public class ArrayList extends AbstractList
implements List, RandomAccess, Cloneable, java.io.Serializable
private transient Object[] elementData;//實(shí)則為數(shù)組
? ? ? ? private int size;
public ArrayList() {//默認(rèn)長(zhǎng)度10
this(10);
}
public ArrayList(Collection c) {
elementData = c.toArray(); //現(xiàn)在elementData已經(jīng)可能不是Object[]
size = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
//將另一個(gè)數(shù)組copy到當(dāng)前數(shù)組
? ? ? ? ? ? ? ? ? ? elementData = Arrays.copyOf(elementData, size, Object[].class);
}
public void trimToSize() {
modCount++;
int oldCapacity = elementData.length;//當(dāng)前長(zhǎng)度
? ? ? ? ? ? ? ? if (size < oldCapacity) {
//當(dāng)前數(shù)組長(zhǎng)度大于設(shè)定的size,將超過(guò)size部分的元素截掉
? ? ? ? ? ? ? ? ? ? elementData = Arrays.copyOf(elementData, size);
}
}
//數(shù)組擴(kuò)容/收縮[自我拷貝]
public static T[] copyOf(T[] original, int newLength) {
return (T[]) copyOf(original, newLength, original.getClass());
}
public boolean isEmpty() {
return size == 0;//是否為空板丽,即大小是否為0
}
public boolean contains(Object o) {
return indexOf(o) >= 0;//contains()底層調(diào)用的是indexOf()
}
public Object clone() {
try {
@SuppressWarnings("unchecked")
//淺克隆:只復(fù)制了基本類型字段
? ? ? ? ? ? ? ? ? ? ? ? ArrayList v = (ArrayList) super.clone();//???為什么克隆之后還要將內(nèi)容拷貝過(guò)去
? ? ? ? ? ? ? ? ? ? ? ? //深克陆娌怠:引用類型字段也復(fù)制
? ? ? ? ? ? ? ? ? ? ? ? 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 Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
public T[] toArray(T[] a) {
if (a.length < size) //小于elementData長(zhǎng)度時(shí)熔吗,通過(guò)Arrays.copyOf()復(fù)制出一個(gè)新的數(shù)組
? ? ? ? ? ? ? ? ? ? ? ? return (T[]) Arrays.copyOf(elementData, size, a.getClass());
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
//private:同類, default:同包, protected:不同包子類, public:公共
? ? ? ? ? ? E elementData(int index) {
return (E) elementData[index];
}
public E get(int index) {
rangeCheck(index); //throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
return elementData(index);
}
public E set(int index, E element) {
rangeCheck(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
public boolean add(E e) {
ensureCapacityInternal(size + 1);? // Increments modCount!!
elementData[size++] = e;
return true;
}
private void ensureCapacityInternal(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)//elementData.length:這個(gè)不是數(shù)組實(shí)際長(zhǎng)度昧谊??
? ? ? ? ? ? ? ? ? ? ? ? grow(minCapacity);//超出容量,擴(kuò)容
? ? ? ? ? ? ? ? }
//防溢出
? ? ? ? ? ? private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
// >>位運(yùn)算国夜,右移動(dòng)一位。 整體相當(dāng)于newCapacity =oldCapacity + 0.5 * oldCapacity
// jdk1.7采用位運(yùn)算比以前的計(jì)算方式更快
? ? ? ? ? ? ? ? ? ? int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0) //這里為什么不直接用<【minCapacity可能為負(fù)數(shù)】
? ? ? ? ? ? ? ? ? ? ? ? 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);//自我拷貝
? ? ? ? ? ? ? ? }
public void add(int index, E element) {
rangeCheckForAdd(index);
ensureCapacityInternal(size + 1);? // Increments modCount!!
//將index之后的元素短绸,拷到index+1的位置之后
? ? ? ? ? ? ? ? ? ? System.arraycopy(elementData, index, elementData, index + 1,
size - index);
//空出index的位置车吹,將新元素加進(jìn)來(lái)
? ? ? ? ? ? ? ? ? ? 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; // Let gc do its work
return oldValue;
}
public boolean addAll(Collection c) {
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew);? // Increments modCount
System.arraycopy(a, 0, elementData, size, numNew);//將新加的即可拷到原數(shù)組后面
? ? ? ? ? ? ? ? ? ? size += numNew;
return numNew != 0;
}
public boolean addAll(int index, Collection c) {
rangeCheckForAdd(index);
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(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;
}
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 class Itr implements Iterator {
int cursor;? ? ? // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
//將arrayList被修改的次數(shù)記錄下來(lái),如果Itr遍歷過(guò)程中醋闭,modCount被修改窄驹,直接拋出異常
? ? ? ? ? ? ? ? ? ? ? ? int expectedModCount = modCount;
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
/*靜態(tài)內(nèi)部類的話,只能new一個(gè)外部類對(duì)象了证逻。
若是實(shí)例內(nèi)部類乐埠,可以使用類名限定
例子:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (外部類類名)OuterClass.this就可以得到外部類的引用了。*/
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
//迭代器能過(guò)正常刪除元素的原因囚企,重置expectedModCount
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}