package java.util;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
public class ArrayList extends AbstractList
implements List, RandomAccess, Cloneable, java.io.Serializable
{
//åºååID
private static final long serialVersionUID = 8683452581122892189L;
//éåé»è®¤çåå§å¤§å°
private static final int DEFAULT_CAPACITY = 10;
//妿å¤é¨ä¸ºéå设置çåå§å大å°ä¸º 0ï¼å elementData æå空æ°ç»å¯¹è±¡ EMPTY_ELEMENTDATA
private static final Object[] EMPTY_ELEMENTDATA = {};
//妿å¨åå§åéåæ¶ä½¿ç¨çæ¯æ åæ°çæé 彿°ï¼å elementData æå空æ°ç»å¯¹è±¡ DEFAULTCAPACITY_EMPTY_ELEMENTDATA
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
//å
å«å®é
å
ç´ çæ°ç»
transient Object[] elementData;
//éå大å°
private int size;
//æå®éåçåå§å®¹éï¼ä»¥æ¤æ¥è¿è¡æ°ç»çåå§åæä½
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);
}
}
//使ç¨é»è®¤çåå§å大å°
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
//ä¼ å
¥ä¸ä»½åå§æ°æ®ï¼ä»¥æ¤è¿è¡åå§å
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 {
this.elementData = EMPTY_ELEMENTDATA;
}
}
//ç§»é¤ä¸å
å«å
ç´ çæ°ç»ç©ºé´ï¼å°æ°ç»ç空é´ç¼©åå°åéåå个大å°
public void trimToSize() {
//æ¯å½éåçç»æåçååæ¶ï¼modCount å°±ä¼éå¢
//å½å¨å¯¹éåè¿è¡è¿ä»£æä½æ¶ï¼è¿ä»£å¨ä¼æ£æ¥æ¤åæ°å¼
//å¦ææ£æ¥å°æ¤åæ°çå¼åçååï¼å°±è¯´æå¨è¿ä»£çè¿ç¨ä¸éåçç»æåçäºååï¼å æ¤ä¼ç´æ¥æåºå¼å¸¸
modCount++;
if (size < elementData.length) {
elementData = (size == 0)
? EMPTY_ELEMENTDATA
: Arrays.copyOf(elementData, size);
}
}
//触åéåè¿è¡æ©å®¹æä½ï¼åæ° minCapacity 表示é忩容åçæå°ç©ºé´
//妿å¨åéåè¿è¡èµå¼æä½åå·²ç¥æ°æ®é大å°ï¼åç´æ¥è°ç¨æ¤æ¹æ³è®©éåç´æ¥æ©å®¹å°è¯¥å¤§å°æå©äºæé«éåçè¿è¡æç
//妿æ¯è®©éåå¨èµå¼çè¿ç¨ä¸èªå¨æ©å®¹ï¼åå¯è½ä¼éè¦è¿è¡å¤æ¬¡æ©å®¹æä½ï¼èæ¯æ¬¡æ©å®¹é½éè¦å¤å¶åææ°æ®å°æ°æ°ç»ï¼è¿ä¼å¯¼è´è¿è¡æçéä½
public void ensureCapacity(int minCapacity) {
//1. 妿å½åæ°ç»è¿æªè¿è¡ä»»ä½èµå¼æä½ï¼å³ elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATAï¼åæ°ç»ç©ºé´åªç±åæ° minCapacity å½±å
//2. 妿å½åæ°ç»å·²è¿è¡è¿èµå¼æä½ï¼å³ elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATAï¼åå½åæ°ç»å¤§å°å¯è½è¿æ¯å¨ä½¿ç¨é»è®¤å®¹é DEFAULT_CAPACITY
// 忤æ¶åªæå½ minCapacity å¤§äº DEFAULT_CAPACITY æ¶ï¼æéè¦è¿è¡æ©å®¹
int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
// any size if not default element table
? 0
// larger than default for default empty table. It's already
// supposed to be at default size.
: DEFAULT_CAPACITY;
if (minCapacity > minExpand) {
ensureExplicitCapacity(minCapacity);
}
}
//æ£æ¥å½åçæ°ç»å®¹éï¼minCapacity ç¨äºæå®å½åéè¦çæå°ç©ºé´
//妿æ°ç»å®¹é没æè¾¾å°ä¸å®æ åï¼åéè¦è¿è¡æ©å®¹
private void ensureCapacityInternal(int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
//æ°ç»ç©ºé´è³å°éè¦æ©å®¹å° DEFAULT_CAPACITY
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
//妿å½åæ°ç»å¤§å°çç¡®æ¯æ¯éè¦çæå°ç©ºé´ minCapacity å°ï¼åè¿è¡æ©å®¹
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
//æ°ç»å¯æ©å
å°çæå¤§ç©ºé´
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
//对æ°ç»è¿è¡æ©å®¹
private void grow(int minCapacity) {
//æ©å®¹åçæ°ç»å¤§å°
int oldCapacity = elementData.length;
//oldCapacity >> 1 çå«ä¹å³ä¸ºï¼å° oldCapacity å¼é¤ä»¥ 2
//å³å
å设æ©å®¹åç空é´å¤§å°æ¯åå
ç1.5å
int newCapacity = oldCapacity + (oldCapacity >> 1);
//妿 newCapacity ä¾ç¶æ¯è¾¾ä¸å°æå°ç©ºé´è¦æ±ï¼åç´æ¥å°ç©ºé´æ©å¤§å°ç± minCapacity æå®ç大å°
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
//妿æ©å®¹åçæ°ç»ç©ºé´è¶
åºäºæå¤§å®¹ééå¶ï¼åå°å®¹éå®ä¸º Integer.MAX_VALUE
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
//æå»ºç¬¦å容é大å°çæ°ç»å¹¶å¤å¶åæ°ç»çæ°æ®
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;
}
//夿é忝å¦å
å«å¯¹è±¡ o
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
//è·å对象 o å¨éå䏿å ç第ä¸ä¸ªç´¢å¼ä½ç½®
//妿éåä¸å
å«å¯¹è±¡ oï¼åè¿å -1
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;
}
//è·å对象 o å¨éå䏿å çæåä¸ä¸ªç´¢å¼ä½ç½®
//妿éåä¸å
å«å¯¹è±¡ oï¼åè¿å -1
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;
}
/**
* Returns a shallow copy of this ArrayList instance. (The
* elements themselves are not copied.)
*
* @return a clone of this ArrayList 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);
}
}
//å°éå转æ¢ä¸ºæ°ç»
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
//è¿åå
嫿æå
ç´ å¼çæ°ç»å¯¹è±¡
//æ°ç»ç±»åç±ä¼ å
¥çåæ°ç±»åæ¥å³å®
@SuppressWarnings("unchecked")
public 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;
}
//éè¿ç´¢å¼ç´æ¥è®¿é®æ°ç»
@SuppressWarnings("unchecked")
E elementData(int index) {
return (E) elementData[index];
}
//è·åç´¢å¼ index å¤çå
ç´ å¼
public E get(int index) {
rangeCheck(index);
return elementData(index);
}
//å°ç´¢å¼ index åºçå
ç´ å¼ç½®ä¸º elementï¼å¹¶è¿ååå§æ°å¼
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);
//èµå¼
elementData[size++] = e;
return true;
}
//å°å
ç´ element æ·»å ç´¢å¼ index ä½ç½®
public void add(int index, E element) {
rangeCheckForAdd(index);
//æ£æ¥æ¯å¦éè¦æ©å®¹
ensureCapacityInternal(size + 1);
//å°ç´¢å¼ index åçæææ°å¼ååæ¨ç§»ä¸ä½
System.arraycopy(elementData, index, elementData, index + 1,size - index);
//å° element æå
¥å°ç©ºåºçä½ç½®
elementData[index] = element;
//éå大å°å 1
size++;
}
//ç§»é¤æå®ç´¢å¼å¤çå
ç´ å¼ï¼å¹¶è¿å该å¼
public E remove(int index) {
rangeCheck(index);
modCount++;
//å¾
ç§»é¤çå
ç´ å¼
E oldValue = elementData(index);
//å 为è¦ç§»é¤å
ç´ å¯¼è´éè¦ç§»å¨çå
ç´ æ°é
int numMoved = size - index - 1;
//å 为è¦ç§»é¤çå
ç´ å¯è½åå¥½æ¯æ°ç»æåä¸ä½ï¼æä»¥ numMoved å¯è½ä¸º 0
//æä»¥åªå¨ numMoved > 0 çæ¶åæéè¦å¯¹æ°ç»çå
ç´ å¼è¿è¡ç§»å¨
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index, numMoved);
//ä¸ç®¡æ°ç»æ¯å¦éè¦å¯¹å
ç´ å¼è¿è¡ç§»å¨ï¼æ°ç»çæåä¸ä½é½æ¯æ ææ°æ®äº
//æ¤å¤å°ä¹ç½®ä¸º null 以帮å©GCåæ¶
elementData[--size] = null;
return oldValue;
}
//ç§»é¤éåä¸å
å«ç第ä¸ä½å
ç´ å¼ä¸º o ç对象
//妿å
å«è¯¥å¯¹è±¡ï¼åè¿å true ï¼å¦åè¿å false
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;
//å 为è¦ç§»é¤çå
ç´ å¯è½åå¥½æ¯æ°ç»æåä¸ä½ï¼æä»¥ numMoved å¯è½ä¸º 0
//æä»¥åªå¨ numMoved > 0 çæ¶åæéè¦å¯¹æ°ç»çå
ç´ å¼è¿è¡ç§»å¨
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index, numMoved);
//ä¸ç®¡æ°ç»æ¯å¦éè¦å¯¹å
ç´ å¼è¿è¡ç§»å¨ï¼æ°ç»çæåä¸ä½é½æ¯æ ææ°æ®äº
//æ¤å¤å°ä¹ç½®ä¸º null 以帮å©GCåæ¶
elementData[--size] = null;
}
//æ¸
空éåå
ç´
public void clear() {
modCount++;
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
//åéåæ·»å æ°æ®
//妿å¾
æ·»å çæ°æ®ä¸ä¸ºç©ºåè¿å trueï¼å¦åè¿å false
public boolean addAll(Collection extends E> c) {
Object[] a = c.toArray();
int numNew = a.length;
//æ£æ¥æ¯å¦éè¦æ©å®¹
ensureCapacityInternal(size + numNew);
//å°æ°ç» a å¤å¶å° elementData ç尾端
System.arraycopy(a, 0, elementData, size, numNew);
size += numNew;
return numNew != 0;
}
//仿å®ç´¢å¼å¤æ·»å æ°æ®
//妿å¾
æ·»å çæ°æ®ä¸ä¸ºç©ºåè¿å trueï¼å¦åè¿å false
public boolean addAll(int index, Collection extends E> c) {
rangeCheckForAdd(index);
Object[] a = c.toArray();
int numNew = a.length;
//æ£æ¥æ¯å¦éè¦æ©å®¹
ensureCapacityInternal(size + numNew);
//éè¦ç§»å¨çæ°ç»å
ç´ æ°é
int numMoved = size - index;
//å ä¸ºè¦æ·»å çæ°æ®å¯è½å好æ¯ä»æ°ç»æå°¾ç«¯å¼å§æ·»å ï¼æä»¥ numMoved å¯è½ä¸º 0
//æä»¥åªå¨ numMoved > 0 çæ¶åæéè¦å¯¹æ°ç»çå
ç´ å¼è¿è¡ç§»å¨ï¼ä»¥æ¤ç©ºåºä½ç½®ç»æ°ç» a
if (numMoved > 0)
System.arraycopy(elementData, index, elementData, index + numNew, numMoved);
//å°æ°ç» a å
å«çæ°æ®æ·»å å° elementData ä¸
System.arraycopy(a, 0, elementData, index, numNew);
size += numNew;
return numNew != 0;
}
//ç§»é¤ä¸¤ä¸ªç´¢å¼å¼ä¹é´çæææ°æ®
//å
å« fromIndex 对åºçå
ç´ ï¼ä¸å
å« toIndex 对åºçå
ç´
protected void removeRange(int fromIndex, int toIndex) {
modCount++;
//éè¦ç§»å¨çæ°ç»å
ç´ æ°é
//æ¤å¤æ¯ç´æ¥å° toIndex åçæ°ç»å
ç´ ååç§»å¨ï¼ä»¥æ¤æ¥è¦ç fromIndex å° toIndex ä¹é´çæ°æ®
int numMoved = size - toIndex;
System.arraycopy(elementData, toIndex, elementData, fromIndex, numMoved);
//ç§»é¤æ°æ®åéåç大å°
int newSize = size - (toIndex-fromIndex);
//å°æ ææ°æ®ç½®ä¸º nullï¼å¸®å©GCåæ¶
for (int i = newSize; i < size; i++) {
elementData[i] = null;
}
size = newSize;
}
//æ£æ¥ç´¢å¼å¼çæææ§
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
//æ£æ¥ç´¢å¼å¼çæææ§
private void rangeCheckForAdd(int index) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
/**
* 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;
}
//ç§»é¤éå䏿æ c å
å«çç¸åå
ç´
//å¦æææ¹å¨å°éåï¼åè¿å trueï¼å¦åè¿å false
public boolean removeAll(Collection> c) {
Objects.requireNonNull(c);
return batchRemove(c, false);
}
//åªä¿çéåå c å
å«çç¸åæ°æ®
//å¦æææ¹å¨å°éåï¼åè¿å trueï¼å¦åè¿å false
public boolean retainAll(Collection> c) {
Objects.requireNonNull(c);
return batchRemove(c, true);
}
//对éåè¿è¡æ°æ®è¿æ»¤
//å¦æææ¹å¨å°éåï¼åè¿å trueï¼å¦åè¿å false
private boolean batchRemove(Collection> c, boolean complement) {
final Object[] elementData = this.elementData;
int r = 0, w = 0;
boolean modified = false;
try {
for (; r < size; r++)
//elementData ä¸ç¬¦åæ¡ä»¶çå
ç´ å°éæ¸ä»å°¾é¨å头é¨éä¸
if (c.contains(elementData[r]) == complement)
elementData[w++] = elementData[r];
} finally {
//r != size çæ
åµåºè¯¥æ¯å¨åçäºå¼å¸¸æ¶æä¼åºç°
//æ¤å¤ç´æ¥å°æªéåå°çå
ç´ æ·»å å elementData 尾端
if (r != size) {
System.arraycopy(elementData, r,
elementData, w,
size - r);
w += size - r;
}
//w != size 说ææå
ç´ è¢«ç§»é¤äº
if (w != size) {
//æ¸
ç©ºæ ææ°æ®ï¼å¸®å©GCåæ¶
for (int i = w; i < size; i++)
elementData[i] = null;
modCount += size - w;
size = w;
modified = true;
}
}
return modified;
}
/**
* Save the state of the ArrayList instance to a stream (that
* is, serialize it).
*
* @serialData The length of the array backing the ArrayList
* instance is emitted (int), followed by all of its elements
* (each an Object) in the proper order.
*/
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 size as capacity for behavioural compatibility with clone()
s.writeInt(size);
// Write out all elements in the proper order.
for (int i=0; iArrayList instance from a stream (that is,
* deserialize it).
*/
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
elementData = EMPTY_ELEMENTDATA;
// Read in size, and any hidden stuff
s.defaultReadObject();
// Read in capacity
s.readInt(); // ignored
if (size > 0) {
// be like clone(), allocate array based upon size not capacity
ensureCapacityInternal(size);
Object[] a = elementData;
// Read in all elements in the proper order.
for (int i=0; i listIterator(int index) {
if (index < 0 || index > size)
throw new IndexOutOfBoundsException("Index: "+index);
return new ListItr(index);
}
/**
* Returns a list iterator over the elements in this list (in proper
* sequence).
*
* The returned list iterator is fail-fast.
*
* @see #listIterator(int)
*/
public ListIterator listIterator() {
return new ListItr(0);
}
//è¿åéåè¿ä»£å¨
public Iterator iterator() {
return new Itr();
}
//ä¸ä¸ªä¼åçæ¬çè¿ä»£å¨
private class Itr implements Iterator {
//lastRet æåçå
ç´ çä¸ä¸ä¸ªå
ç´ çç´¢å¼
int cursor;
//æåä¸ä¸ªè¿åçå
ç´ çç´¢å¼
//妿å¼ä¸º -1ï¼è¯´æè¿æªè¿åè¿å
ç´ æè
æ¹å
ç´ è¢«ç§»é¤äº
int lastRet = -1;
//ç¨äºéªè¯éåçæ°æ®ç»æå¨è¿ä»£çè¿ç¨ä¸æ¯å¦è¢«ä¿®æ¹äº
int expectedModCount = modCount;
//æ¯å¦è¿æå
ç´ æªè¢«éå
public boolean hasNext() {
return cursor != size;
}
//è·åä¸ä¸ä¸ªå
ç´
@SuppressWarnings("unchecked")
public E next() {
checkForComodification();
int i = cursor;
//å¦æç´¢å¼å¼è¶
åºéåçå¯ç´¢å¼èå´åæåºå¼å¸¸
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
//å¦æç´¢å¼å¼è¶
åºæ°ç»çå¯ç´¢å¼èå´åæåºå¼å¸¸
if (i >= elementData.length)
throw new ConcurrentModificationException();
//游æ éå¢
cursor = i + 1;
return (E) elementData[lastRet = i];
}
//ç§»é¤ lastRet ä½ç½®çå
ç´
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.remove(lastRet);
//å 为 lastRet ä½ç½®åå§çå
ç´ è¢«ç§»é¤äºï¼æä»¥æ¤æ¶ lastRet æåçå
ç´ æ¯åå
lastRet+1 ä½ç½®çå
ç´
cursor = lastRet;
lastRet = -1;
//å ä¸ºæ¯ Itr 主å¨å¯¹éåè¿è¡ä¿®æ¹ï¼æä»¥æ¤å¤éè¦ä¸»å¨æ´æ° expectedModCount å¼ï¼é¿å
ä¹åæåºå¼å¸¸
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
//éåéåä»ç´¢å¼ cursor å¼å§ä¹åå©ä¸çå
ç´
@Override
@SuppressWarnings("unchecked")
public void forEachRemaining(Consumer super E> consumer) {
Objects.requireNonNull(consumer);
final int size = ArrayList.this.size;
int i = cursor;
if (i >= size) {
return;
}
final Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length) {
throw new ConcurrentModificationException();
}
//éåè°ç¨ accept æ¹æ³
while (i != size && modCount == expectedModCount) {
consumer.accept((E) elementData[i++]);
}
cursor = i;
lastRet = i - 1;
checkForComodification();
}
//夿è¿ä»£å¨å¨éåéåçè¿ç¨ä¸ï¼é忝å¦è¢«å¤é¨æ¹å¨äºï¼ä¾å¦è¢«å
¶å®è¿ä»£å¨ç§»é¤äºå
ç´ ï¼
//妿æ¯çè¯åæåºå¼å¸¸
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
private class ListItr extends Itr implements ListIterator {
ListItr(int index) {
super();
cursor = index;
}
public boolean hasPrevious() {
return cursor != 0;
}
public int nextIndex() {
return cursor;
}
public int previousIndex() {
return cursor - 1;
}
@SuppressWarnings("unchecked")
public E previous() {
checkForComodification();
int i = cursor - 1;
if (i < 0)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i;
return (E) elementData[lastRet = i];
}
public void set(E e) {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.set(lastRet, e);
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
public void add(E e) {
checkForComodification();
try {
int i = cursor;
ArrayList.this.add(i, e);
cursor = i + 1;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
}
//è·åç±åæ°å¼æå®èå´çåéå
//å
¶å®æ¯å¯¹éåå
¶æå®èå´å
çå
ç´ è¿è¡å
è£
ï¼æå¯¹åéåçæä½æ å°å°ç¶éåä¸
public List subList(int fromIndex, int toIndex) {
subListRangeCheck(fromIndex, toIndex, size);
return new SubList(this, 0, fromIndex, toIndex);
}
static void subListRangeCheck(int fromIndex, int toIndex, int size) {
if (fromIndex < 0)
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
if (toIndex > size)
throw new IndexOutOfBoundsException("toIndex = " + toIndex);
if (fromIndex > toIndex)
throw new IllegalArgumentException("fromIndex(" + fromIndex +
") > toIndex(" + toIndex + ")");
}
private class SubList extends AbstractList implements RandomAccess {
//ç¶éå对象
private final AbstractList parent;
//åéåèµ·å§ç´¢å¼å¼ç¸å¯¹ç¶éåçåç§»é
//å¨å¯¹åéåè¿è¡æ·»å å
ç´ åç§»é¤å
ç´ çæä½æ¶ï¼é½éè¦å 䏿¤åç§»é
private final int parentOffset;
//é¢å¤çåç§»é
private final int offset;
//åéåçå
ç´ ä¸ªæ°
int size;
SubList(AbstractList parent,
int offset, int fromIndex, int toIndex) {
this.parent = parent;
this.parentOffset = fromIndex;
this.offset = offset + fromIndex;
this.size = toIndex - fromIndex;
this.modCount = ArrayList.this.modCount;
}
//index 弿¯ç¸å¯¹äºåéåçç´¢å¼ï¼éè¦å°ä¹æ å°å°ç¶éå对象
//SubList çæ¹æ³åºæ¬é½ä¸ ArrayList ç类似ï¼åè¾¹ä¸åèµè¿°
public E set(int index, E e) {
rangeCheck(index);
checkForComodification();
E oldValue = ArrayList.this.elementData(offset + index);
ArrayList.this.elementData[offset + index] = e;
return oldValue;
}
public E get(int index) {
rangeCheck(index);
checkForComodification();
return ArrayList.this.elementData(offset + index);
}
public int size() {
checkForComodification();
return this.size;
}
public void add(int index, E e) {
rangeCheckForAdd(index);
checkForComodification();
parent.add(parentOffset + index, e);
this.modCount = parent.modCount;
this.size++;
}
public E remove(int index) {
rangeCheck(index);
checkForComodification();
E result = parent.remove(parentOffset + index);
this.modCount = parent.modCount;
this.size--;
return result;
}
protected void removeRange(int fromIndex, int toIndex) {
checkForComodification();
parent.removeRange(parentOffset + fromIndex,
parentOffset + toIndex);
this.modCount = parent.modCount;
this.size -= toIndex - fromIndex;
}
public boolean addAll(Collection extends E> c) {
return addAll(this.size, c);
}
public boolean addAll(int index, Collection extends E> c) {
rangeCheckForAdd(index);
int cSize = c.size();
if (cSize==0)
return false;
checkForComodification();
parent.addAll(parentOffset + index, c);
this.modCount = parent.modCount;
this.size += cSize;
return true;
}
public Iterator iterator() {
return listIterator();
}
public ListIterator listIterator(final int index) {
checkForComodification();
rangeCheckForAdd(index);
final int offset = this.offset;
return new ListIterator() {
int cursor = index;
int lastRet = -1;
int expectedModCount = ArrayList.this.modCount;
public boolean hasNext() {
return cursor != SubList.this.size;
}
@SuppressWarnings("unchecked")
public E next() {
checkForComodification();
int i = cursor;
if (i >= SubList.this.size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (offset + i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[offset + (lastRet = i)];
}
public boolean hasPrevious() {
return cursor != 0;
}
@SuppressWarnings("unchecked")
public E previous() {
checkForComodification();
int i = cursor - 1;
if (i < 0)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (offset + i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i;
return (E) elementData[offset + (lastRet = i)];
}
@SuppressWarnings("unchecked")
public void forEachRemaining(Consumer super E> consumer) {
Objects.requireNonNull(consumer);
final int size = SubList.this.size;
int i = cursor;
if (i >= size) {
return;
}
final Object[] elementData = ArrayList.this.elementData;
if (offset + i >= elementData.length) {
throw new ConcurrentModificationException();
}
while (i != size && modCount == expectedModCount) {
consumer.accept((E) elementData[offset + (i++)]);
}
// update once at end of iteration to reduce heap write traffic
lastRet = cursor = i;
checkForComodification();
}
public int nextIndex() {
return cursor;
}
public int previousIndex() {
return cursor - 1;
}
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
SubList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = ArrayList.this.modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
public void set(E e) {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.set(offset + lastRet, e);
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
public void add(E e) {
checkForComodification();
try {
int i = cursor;
SubList.this.add(i, e);
cursor = i + 1;
lastRet = -1;
expectedModCount = ArrayList.this.modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (expectedModCount != ArrayList.this.modCount)
throw new ConcurrentModificationException();
}
};
}
public List subList(int fromIndex, int toIndex) {
subListRangeCheck(fromIndex, toIndex, size);
return new SubList(this, offset, fromIndex, toIndex);
}
private void rangeCheck(int index) {
if (index < 0 || index >= this.size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private void rangeCheckForAdd(int index) {
if (index < 0 || index > this.size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+this.size;
}
private void checkForComodification() {
if (ArrayList.this.modCount != this.modCount)
throw new ConcurrentModificationException();
}
public Spliterator spliterator() {
checkForComodification();
return new ArrayListSpliterator(ArrayList.this, offset,
offset + this.size, this.modCount);
}
}
//éåéåå
ç´
@Override
public void forEach(Consumer super E> action) {
Objects.requireNonNull(action);
final int expectedModCount = modCount;
@SuppressWarnings("unchecked")
final E[] elementData = (E[]) this.elementData;
final int size = this.size;
//妿 modCount å¼è¢«æ¹å¨ï¼åç´æ¥åæ¢éåå¹¶æåºå¼å¸¸
for (int i=0; modCount == expectedModCount && i < size; i++) {
//å°éåå
ç´ ä¾æ¬¡ä¼ éç» accept æ¹æ³
action.accept(elementData[i]);
}
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
/**
* Creates a late-binding
* and fail-fast {@link Spliterator} over the elements in this
* list.
*
* The {@code Spliterator} reports {@link Spliterator#SIZED},
* {@link Spliterator#SUBSIZED}, and {@link Spliterator#ORDERED}.
* Overriding implementations should document the reporting of additional
* characteristic values.
*
* @return a {@code Spliterator} over the elements in this list
* @since 1.8
*/
@Override
public Spliterator spliterator() {
return new ArrayListSpliterator<>(this, 0, -1, 0);
}
/** Index-based split-by-two, lazily initialized Spliterator */
static final class ArrayListSpliterator implements Spliterator {
private final ArrayList list;
private int index; // current index, modified on advance/split
private int fence; // -1 until used; then one past last index
private int expectedModCount; // initialized when fence set
/** Create new spliterator covering the given range */
ArrayListSpliterator(ArrayList list, int origin, int fence,
int expectedModCount) {
this.list = list; // OK if null unless traversed
this.index = origin;
this.fence = fence;
this.expectedModCount = expectedModCount;
}
private int getFence() { // initialize fence to size on first use
int hi; // (a specialized variant appears in method forEach)
ArrayList lst;
if ((hi = fence) < 0) {
if ((lst = list) == null)
hi = fence = 0;
else {
expectedModCount = lst.modCount;
hi = fence = lst.size;
}
}
return hi;
}
public ArrayListSpliterator trySplit() {
int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
return (lo >= mid) ? null : // divide range in half unless too small
new ArrayListSpliterator(list, lo, index = mid,
expectedModCount);
}
public boolean tryAdvance(Consumer super E> action) {
if (action == null)
throw new NullPointerException();
int hi = getFence(), i = index;
if (i < hi) {
index = i + 1;
@SuppressWarnings("unchecked") E e = (E)list.elementData[i];
action.accept(e);
if (list.modCount != expectedModCount)
throw new ConcurrentModificationException();
return true;
}
return false;
}
public void forEachRemaining(Consumer super E> action) {
int i, hi, mc; // hoist accesses and checks from loop
ArrayList lst; Object[] a;
if (action == null)
throw new NullPointerException();
if ((lst = list) != null && (a = lst.elementData) != null) {
if ((hi = fence) < 0) {
mc = lst.modCount;
hi = lst.size;
}
else
mc = expectedModCount;
if ((i = index) >= 0 && (index = hi) <= a.length) {
for (; i < hi; ++i) {
@SuppressWarnings("unchecked") E e = (E) a[i];
action.accept(e);
}
if (lst.modCount == mc)
return;
}
}
throw new ConcurrentModificationException();
}
public long estimateSize() {
return (long) (getFence() - index);
}
public int characteristics() {
return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED;
}
}
//æç
§ç»å®è§å对éåå
ç´ è¿è¡è¿æ»¤ï¼å¦æå
ç´ ç¬¦åè¿æ»¤è§å filter åå°ä¹ç§»é¤
@Override
public boolean removeIf(Predicate super E> filter) {
Objects.requireNonNull(filter);
//è¦ç§»é¤çå
ç´ ä¸ªæ°
int removeCount = 0;
//ç¨äºæ è®°é忝åªä¸ªç´¢å¼ä½ç½®éè¦è¢«ç§»é¤
final BitSet removeSet = new BitSet(size);
final int expectedModCount = modCount;
final int size = this.size;
for (int i=0; modCount == expectedModCount && i < size; i++) {
@SuppressWarnings("unchecked")
final E element = (E) elementData[i];
//便¬¡å¤æéåå
ç´ æ¯å¦ç¬¦åè¿æ»¤è§å
if (filter.test(element)) {
//set æ¹æ³å°å¯¼è´ç´¢å¼ä½ç½® i çå
ç´ å为 true
removeSet.set(i);
removeCount++;
}
}
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
//åªæ removeCount > 0 æè¯´æéè¦ç§»é¤å
ç´
final boolean anyToRemove = removeCount > 0;
if (anyToRemove) {
//éåç§»é¤æå®å
ç´ åç大å°
final int newSize = size - removeCount;
for (int i=0, j=0; (i < size) && (j < newSize); i++, j++) {
//ç¥è¿è¢«æ 记为 true çä½ç½®ï¼ç´æ¥è·³å°ä¸éè¦ç§»é¤å
ç´ çæ°ç»ç´¢å¼ä½
i = removeSet.nextClearBit(i);
//æææ°æ®éæ¸ä»å°¾é¨å头é¨èé
elementData[j] = elementData[i];
}
//ç§»é¤å°¾é¨çæ ææ°æ®ï¼å¸®å©GCåæ¶
for (int k=newSize; k < size; k++) {
elementData[k] = null;
}
this.size = newSize;
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
modCount++;
}
return anyToRemove;
}
//å°éåå
ç´ éåä¼ éç» operatorï¼å¹¶å°åå§æ°æ®æ¿æ¢ä¸º operator çè¿åå¼
@Override
@SuppressWarnings("unchecked")
public void replaceAll(UnaryOperator operator) {
Objects.requireNonNull(operator);
final int expectedModCount = modCount;
final int size = this.size;
for (int i=0; modCount == expectedModCount && i < size; i++) {
//便¬¡ä¼ éæ°ç»å
ç´ ç» apply æ¹æ³ï¼å¹¶å°å
¶è¿å弿¿æ¢åå§æ°æ®
elementData[i] = operator.apply((E) elementData[i]);
}
//ä¸å
è®¸å¨æåºçè¿ç¨ä¸éå被å
¶å®æ¹æ³ä¿®æ¹äºæ°æ®ç»æï¼ä¾å¦ï¼ç§»é¤å
ç´ ï¼
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
modCount++;
}
//å°éåæç
§æå®è§åè¿è¡æåº
@Override
@SuppressWarnings("unchecked")
public void sort(Comparator super E> c) {
final int expectedModCount = modCount;
Arrays.sort((E[]) elementData, 0, size, c);
//ä¸å
è®¸å¨æåºçè¿ç¨ä¸éå被å
¶å®æ¹æ³ä¿®æ¹äºæ°æ®ç»æï¼ä¾å¦ï¼ç§»é¤å
ç´ ï¼
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
modCount++;
}
}