通过阅读源码 熟悉API-ArrayList-基于jdk1.8
增加
public boolean add ( E e)
public void add ( int index, E element)
public boolean addAll ( Collection< ? extends E > c)
public boolean addAll ( int index, Collection< ? extends E > c)
private void rangeCheckForAdd ( int index) {
throw new IndexOutOfBoundsException
}
private void ensureCapacityInternal ( int minCapacity) {
}
private void ensureExplicitCapacity ( int minCapacity) {
grow ( minCapacity) {
* * int newCapacity = oldCapacity + ( oldCapacity >> 1 ) ; * *
} ;
}
删除
public E remove ( int index) {
}
public boolean remove ( Object o) {
}
public boolean removeAll ( Collection< ? > c)
public boolean removeIf ( Predicate< ? super E> filter) {
if ( filter. test ( element) ) {
removeSet. set ( i) ;
removeCount++ ;
}
for ( int i= 0 , j= 0 ; ( i < size) && ( j < newSize) ; i++ , j++ ) {
* * i = removeSet. nextClearBit ( i) ; * *
elementData[ j] = elementData[ i] ;
}
}
private void fastRemove ( int index) {
}
private boolean batchRemove ( Collection< ? > c, boolean complement) {
if ( c. contains ( elementData[ r] ) == complement)
if ( r != size) {
System. arraycopy ( elementData, r,
elementData, w,
size - r) ;
w += size - r;
}
}
修改
public E set ( int index, E element)
public void replaceAll ( UnaryOperator< E> operator)
查找
public int indexOf ( Object o)
public void forEach ( Consumer< ? super E> action) {
for ( int i= 0 ; modCount == expectedModCount && i < size; i++ ) {
action. accept ( elementData[ i] ) ;
}
}
排序
public void sort ( Comparator< ? super E> c) {
}