Java源码阅读之ArrayList上
方法
public ListIterator<E> listIterator(int index){
if(index < 0 || index > szie){
throw new IndexOutOfBoundsException("Index: "+index);
}
return new ListItr(index);
}
public ListIterator<E> listIterator(){
return new ListItr(0);
}
public Iterator<E> iterator(){
return new Itr();
}
private class Itr implements Iterator<E>{
int cursor;
int lastRet = -1;
int expectedModCount = modCount;
Itr(){
}
public boolean hasNext(){
return cursor != size;
}
public E next(){
checkForModification();
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];
}
public void remove(){
if(lastRet < 0){
throw new IllegalStateException();
}
checkForModification();
try{
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
}catch(IndexOutOfBoundsException e){
throw new ConcurrentModificationException();
}
}
public void foreachRemaining(Consumer<? super E> consumer){
Objects.requireNonNull(consumer);
final int size = ArrayList.this.size;
int i = cursor;
if(i >= cursor)
return;
final Object[] elementData = ArrayList.this.elementData;
if(i >= elementData.length)
throw new ConcurrentModificationException();
while(i != size && exceptedModeCount == modCount)
consumer.accept((E) elementData[i++]);
cursor = i;
lastRet = i - 1;
checkForComodification();
}
final void checkForComodification(){
if(exceptedModCount != modCount)
throw new ConcurrentModificationException();
}
}
private class ListItr extends Itr implements ListIterator<E>{
ListItr(int index){
super();
cursor = index;
}
public boolean hasPrevious(){
return cursor != 0;
}
public int nextIndex(){
return cursor;
}
public int previousIndex(){
return cursor - 1;
}
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[i];
}
public void set(E e){
if(lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try{
ArrayList.this.set(lastRet, e);
}catch(IndexOutOfBoundsException e){
throw new ConcurrentModificationException();
}
}
public void add(E e){
try{
int i = cursor;
ArrayList.this.add(i, e);
cursor = i + 1;
lastRet = -1;
expectedModCount = modCount;
}catch(IndexOutOfBoundsException e){
throw new ConcurrentModificationException();
}
}
public void forEach(Consumer<? super E> consumer){
Objects.requireNonNull(consumer);
final int expectedModCount = modCount;
final E[] elementData = (E[]) this.elementData;
final int size = this.size;
for(int i = 0; expectedModCount == modCount && i < size; i++)
consumer.accpet(elementData[i]);
if(expectedModCount != modCount)
throw new ConcurrentModificationException();
}
public Spliterator<E> spliterator(){
return new ArrayListSpliterator<>(this, 0, -1, 0);
}
}
static final class ArrayListSpliterator<E> implements Spliterator<E>{
private final ArrayList<E> list;
private int index;
private int fence;
private int expectedModCount;
ArrayListSpliterator(ArrayList<E> list, int origin, int fence, int expectedModCount){
this.list = list;
this.index = origin;
this.fence = fence;
this.expectedModCount = expectedModCount;
}
private int genFence(){
int hi;
ArrayList<E> lst;
if((hi = fence) < 0){
if((lst = list) == null)
hi = fence = 0;
else
this.expectedModCount = lst.modCount;
hi = fence = lst.size;
}
return hi;
}
private ArrayListSpliterator<E> trySplit(){
int hi = getFence(), lo = index, mid = (hi + lo) >>> 1;
return (lo >= mid) ? null : new ArrayListSpliterator<E>(list, lo , index = mid, expectedModCount);
}
public boolean tryAdvance(Consumer<? super E> consumer){
if(consumer == null)
throw new NullPointerException();
int hi = getFence(), i = index;
if(i < hi){
index = i + 1;
E e = (E)list.elementData[i];
consumer.accpet(e);
if(list.modCount != expectModCount)
throw new ConcurrentModificationException();
return true;
}
return false;
}
public void forEachRemaining(Comsumer<? super E> action){
int i, hi, mc;
ArrayList<E> lst, Object[] a;
if(action == null)
throw new NullPointerException();
if((lst = list) != null && (a = list.elementData) != null){
if((hi = fence) < 0){
hi = list.size;
mc = list.modCount;
}else{
mc = exceptedModCount;
}
if((i = index) >= 0 && (index = hi) <= a.length){
for(; i < hi; i++){
E e = (E) a[i];
action.accept(e);
}
if(lst.modCount == mc)
return;
}
}
throw new ConcurrentModificationException();
}
}
public boolean removeIf(Predicate<? super E> filter){
Objects.requireNonNull(filter);
}
public void replaceAll(UnaryOperator<E> operator){
Objects.requireNonNull(operator);
final int expectedModCount = modCount;
final int size = this.size;
for(int i = 0; i < size && modCount == expectedModCount; i++){
elementData[i] = operator.apply((E) elementData[i]);
}
if(modCount != expectedModCount)
throw new ConcurrentModificationException();
modCount++;
}
public void sort(Comparator<? extends E> c){
final int expectedModCount = modCount;
Arrays.sort((E[]) elementData, 0, size,c);
if(modCount != expectedModCount){
throw new ConcurrentModificationException();
}
modCount ++;
}