ArrayList
private static final long serialVersionUID = 8683452581122892189 L;
private static final int DEFAULT_CAPACITY = 10 ;
private static final Object[ ] 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) {
Object[ ] a = c. toArray ( ) ;
if ( ( size = a. length) != 0 ) {
if ( c. getClass ( ) == ArrayList. class ) {
elementData = a;
} else {
elementData = Arrays. copyOf ( a, size, Object[ ] . class ) ;
}
} else {
elementData = EMPTY_ELEMENTDATA;
}
}
public void trimToSize ( ) {
modCount++ ;
if ( size < elementData. length) {
elementData = ( size == 0 )
? EMPTY_ELEMENTDATA
: Arrays. copyOf ( elementData, size) ;
}
}
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 ;
}
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 ;
}
public Object clone ( ) {
try {
ArrayList< ? > v = ( ArrayList< ? > ) super . clone ( ) ;
v. elementData = Arrays. copyOf ( elementData, size) ;
v. modCount = 0 ;
return v;
} catch ( CloneNotSupportedException e) {
throw new InternalError ( e) ;
}
}
public < T> T[ ] toArray ( T[ ] a) {
if ( a. length < size)
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 E set ( int index, E element) {
rangeCheck ( index) ;
E oldValue = elementData ( index) ;
elementData[ index] = element;
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;
}
public boolean addAll ( Collection< ? extends E > c) {
Object[ ] a = c. toArray ( ) ;
int numNew = a. length;
ensureCapacityInternal ( size + numNew) ;
System. arraycopy ( a, 0 , elementData, size, numNew) ;
size += numNew;
return numNew != 0 ;
}
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;
if ( numMoved > 0 )
System. arraycopy ( elementData, index, elementData, index + numNew,
numMoved) ;
System. arraycopy ( a, 0 , elementData, index, numNew) ;
size += numNew;
return numNew != 0 ;
}
add(E e)
public boolean add ( E e) {
ensureCapacityInternal ( size + 1 ) ;
elementData[ size++ ] = e;
return true ;
}
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++ ;
if ( minCapacity - elementData. length > 0 )
grow ( minCapacity) ;
}
private void grow ( int minCapacity) {
int oldCapacity = elementData. length;
int newCapacity = oldCapacity + ( oldCapacity >> 1 ) ;
if ( newCapacity - minCapacity < 0 )
newCapacity = minCapacity;
if ( newCapacity - MAX_ARRAY_SIZE > 0 )
newCapacity = hugeCapacity ( minCapacity) ;
elementData = Arrays. copyOf ( elementData, newCapacity) ;
}
add(int index, E element)
public void add ( int index, E element) {
rangeCheckForAdd ( index) ;
ensureCapacityInternal ( size + 1 ) ;
System. arraycopy ( elementData, index, elementData, index + 1 ,
size - index) ;
elementData[ index] = element;
size++ ;
}
private void rangeCheckForAdd ( int index) {
if ( index > size || index < 0 )
throw new IndexOutOfBoundsException ( outOfBoundsMsg ( index) ) ;
}
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++ ;
if ( minCapacity - elementData. length > 0 )
grow ( minCapacity) ;
}
private void grow ( int minCapacity) {
int oldCapacity = elementData. length;
int newCapacity = oldCapacity + ( oldCapacity >> 1 ) ;
if ( newCapacity - minCapacity < 0 )
newCapacity = minCapacity;
if ( newCapacity - MAX_ARRAY_SIZE > 0 )
newCapacity = hugeCapacity ( minCapacity) ;
elementData = Arrays. copyOf ( elementData, newCapacity) ;
}
remove(int index)
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 , eleaamentData, index,
numMoved) ;
elementData[ -- size] = null;
return oldValue;
}
private void rangeCheck ( int index) {
if ( index >= size)
throw new IndexOutOfBoundsException ( outOfBoundsMsg ( index) ) ;
}