ArrayList
ArrayList
ArrayList
ArrayList
ArrayList
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
RandomAccess
ArrayList
//
private static final long serialVersionUID = 8683452581122892189L;
// ArrayList
private static final int DEFAULT_CAPACITY = 10;
//
private static final Object[] EMPTY_ELEMENTDATA = {};
//
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
//
transient Object[] elementData; // non-private to simplify nested class access
// list
private int size;
ArrayList
//
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() {
// elementData
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
//
//
// c
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 {
// replace with empty array.
this.elementData = EMPTY_ELEMENTDATA;
}
}
ArrayList
add()
//
public boolean add(E e) {
//
ensureCapacityInternal(size + 1); // Increments modCount!!
//
elementData[size++] = e;
//
return true;
}
add()
//
private void ensureCapacityInternal(int minCapacity) {
//
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
//
private static int calculateCapacity(Object[] elementData, int minCapacity) {
//
//
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
//
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity;
}
//
private void ensureExplicitCapacity(int minCapacity) {
//
modCount++;
// overflow-conscious code
//
if (minCapacity - elementData.length > 0)
//
grow(minCapacity);
}
grow
// grow
private void grow(int minCapacity) {
// overflow-conscious code
//
int 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);
}
grow
//
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 void add(int index, E element) {
//
rangeCheckForAdd(index);
//
ensureCapacityInternal(size + 1); // Increments modCount!!
//
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
//
elementData[index] = element;
size++;
}
rangeCheckForAdd
//
private void rangeCheckForAdd(int index) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
//
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;
// size
this.size += cSize;
return true;
}
private void checkForComodification() {
//
if (ArrayList.this.modCount != this.modCount)
//
throw new ConcurrentModificationException();
}
//
public E get(int index) {
//
rangeCheck(index);
//
return elementData(index);
}
private void rangeCheck(int index) {
//
if (index >= size)
//
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
//
E elementData(int index) {
return (E) elementData[index];
}
public E set(int index, E element) {
//
rangeCheck(index);
//
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
//
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; // clear to let GC do its work
//
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;
}
fastRemove
/**
*
*/
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; // clear to let GC do its work
}