/**
* Default initial capacity.
*/privatestaticfinalint DEFAULT_CAPACITY = 10;
/**
* Shared empty array instance used for empty instances.
*/privatestaticfinal 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.
*/privatestaticfinal 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.
*/// Android-note: Also accessed from java.util.Collectionstransient Object[] elementData; // non-private to simplify nested class access/**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/privateint size;
複製程式碼
/**
* Constructs an empty list with the specified initial capacity.
*
* @param initialCapacity the initial capacity of the list
* @throws IllegalArgumentException if the specified initial capacity
* is negative
*/publicArrayList(int initialCapacity){
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} elseif (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
thrownew IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
/**
* Constructs an empty list with an initial capacity of ten.
*/publicArrayList(){
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
複製程式碼
privatevoidgrow(int minCapacity){
// overflow-conscious codeint 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);
}
複製程式碼
public E get(int index){
if (index >= size)
thrownew IndexOutOfBoundsException(outOfBoundsMsg(index));
return (E) elementData[index];
}
複製程式碼
public E set(int index, E element){
if (index >= size)
thrownew IndexOutOfBoundsException(outOfBoundsMsg(index));
E oldValue = (E) elementData[index];
elementData[index] = element;
return oldValue;
}
複製程式碼
publicbooleanadd(E e){
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
returntrue;
}
/**
* 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}
*/publicvoidadd(int index, E element){
if (index > size || index < 0)
thrownew IndexOutOfBoundsException(outOfBoundsMsg(index));
ensureCapacityInternal(size + 1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
複製程式碼
public E remove(int index){
if (index >= size)
thrownew IndexOutOfBoundsException(outOfBoundsMsg(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; // clear to let GC do its workreturn 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
*/publicbooleanremove(Object o){
if (o == null) {
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
returntrue;
}
} else {
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
returntrue;
}
}
returnfalse;
}
/*
* Private remove method that skips bounds checking and does not
* return the value removed.
*/privatevoidfastRemove(int index){
modCount++;
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work
}
複製程式碼
/**
* Returns a list iterator over the elements in this list (in proper
* sequence).
*
* <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
*
* @see #listIterator(int)
*/public ListIterator<E> listIterator(){
returnnew ListItr(0);
}
/**
* Returns an iterator over the elements in this list in proper sequence.
*
* <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
*
* @return an iterator over the elements in this list in proper sequence
*/public Iterator<E> iterator(){
returnnew Itr();
}
複製程式碼
privateclassItrimplementsIterator<E> {
// Android-changed: Add "limit" field to detect end of iteration.// The "limit" of this iterator. This is the size of the list at the time the// iterator was created. Adding & removing elements will invalidate the iteration// anyway (and cause next() to throw) so saving this value will guarantee that the// value of hasNext() remains stable and won't flap between true and false when elements// are added and removed from the list.protectedint limit = ArrayList.this.size;
int cursor; // index of next element to returnint lastRet = -1; // index of last element returned; -1 if no suchint expectedModCount = modCount;
publicbooleanhasNext(){
return cursor < limit;
}
@SuppressWarnings("unchecked")
public E next(){
if (modCount != expectedModCount)
thrownew ConcurrentModificationException();
int i = cursor;
if (i >= limit)
thrownew NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
thrownew ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
publicvoidremove(){
if (lastRet < 0)
thrownew IllegalStateException();
if (modCount != expectedModCount)
thrownew ConcurrentModificationException();
try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
limit--;
} catch (IndexOutOfBoundsException ex) {
thrownew ConcurrentModificationException();
}
}
@Override@SuppressWarnings("unchecked")
publicvoidforEachRemaining(Consumer<? super E> consumer){
Objects.requireNonNull(consumer);
finalint size = ArrayList.this.size;
int i = cursor;
if (i >= size) {
return;
}
final Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length) {
thrownew ConcurrentModificationException();
}
while (i != size && modCount == expectedModCount) {
consumer.accept((E) elementData[i++]);
}
// update once at end of iteration to reduce heap write traffic
cursor = i;
lastRet = i - 1;
if (modCount != expectedModCount)
thrownew ConcurrentModificationException();
}
}
複製程式碼
public E remove(int index){
if (index >= size)
thrownew IndexOutOfBoundsException(outOfBoundsMsg(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; // clear to let GC do its workreturn oldValue;
}
複製程式碼