/** * 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. * 与 EMPTY_ELEMENTDATA 区分开的目的是,添加第一个元素的时候可以确定数组的指定长度 */ privatestaticfinal Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
privatevoidgrow(int minCapacity){ // overflow-conscious code int oldCapacity = elementData.length; //oldCapacity >> 1和oldCapacity / 2是等效的,newCapacity为原来的1.5倍 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: //进行数组拷贝,Arrays.copyOf的底层是一个native方法 elementData = Arrays.copyOf(elementData, newCapacity); }
privatevoidrangeCheck(int index){ if (index >= size) thrownew IndexOutOfBoundsException(outOfBoundsMsg(index)); }
/** * A version of rangeCheck used by add and addAll. */ privatevoidrangeCheckForAdd(int index){ if (index > size || index < 0) thrownew IndexOutOfBoundsException(outOfBoundsMsg(index)); }
@SuppressWarnings("unchecked") E elementData(int index){ return (E) elementData[index]; }
int numMoved = size - index - 1; if (numMoved > 0) //直接进行数组拷贝操作,把index后的所有元素向前移动一位。 System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // clear to let GC do its work
return oldValue; }
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; }
//之所以叫做快速删除,是因为他被设置为一个私有方法,只能在内部调用,删除元素的时候,省去了数组越界的判断。 //也不返回被删除的元素,直接进行数组拷贝操作。 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 }
//Iterable 接口的方法实现 public Iterator<E> iterator(){ returnnew Itr(); }
//An optimized version of AbstractList.Itr //大家可以看下 AbstractList 的 Itr 实现 privateclassItrimplementsIterator<E> { // 游标 int cursor; // index of next element to return int lastRet = -1; // index of last element returned; -1 if no such //集合修改标志 //关于快速失败机制的实现,稍后会有讲解 int expectedModCount = modCount;
Itr() {}
publicbooleanhasNext(){ return cursor != size; }
@SuppressWarnings("unchecked") public E next(){ // 检查集合是否被修改 checkForComodification(); int i = cursor; if (i >= size) thrownew NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) thrownew ConcurrentModificationException(); cursor = i + 1; return (E) elementData[lastRet = i]; }
@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; checkForComodification(); }
finalvoidcheckForComodification(){ if (modCount != expectedModCount) thrownew ConcurrentModificationException(); } }
// List 接口的方法实现 public ListIterator<E> listIterator(){ returnnew ListItr(0); }
// 在指定位置处遍历 public ListIterator<E> listIterator(int index){ if (index < 0 || index > size) thrownew IndexOutOfBoundsException("Index: "+index); returnnew ListItr(index); }
@SuppressWarnings("unchecked") public E previous(){ checkForComodification(); // 向前遍历,游标 - 1 int i = cursor - 1; if (i < 0) thrownew NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) thrownew ConcurrentModificationException(); cursor = i; return (E) elementData[lastRet = i]; }
//删除指定位置的元素 public E remove(int index){ rangeCheck(index); checkForComodification(); E result = parent.remove(parentOffset + index); this.modCount = parent.modCount; this.size--; return result; }
@SuppressWarnings("unchecked") public E next(){ checkForComodification(); int i = cursor; if (i >= SubList.this.size) thrownew NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (offset + i >= elementData.length) thrownew ConcurrentModificationException(); cursor = i + 1; return (E) elementData[offset + (lastRet = i)]; }
publicbooleanhasPrevious(){ return cursor != 0; }
@SuppressWarnings("unchecked") public E previous(){ checkForComodification(); int i = cursor - 1; if (i < 0) thrownew NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (offset + i >= elementData.length) thrownew ConcurrentModificationException(); cursor = i; return (E) elementData[offset + (lastRet = i)]; }
@SuppressWarnings("unchecked") publicvoidforEachRemaining(Consumer<? super E> consumer){ Objects.requireNonNull(consumer); finalint size = SubList.this.size; int i = cursor; if (i >= size) { return; } final Object[] elementData = ArrayList.this.elementData; if (offset + i >= elementData.length) { thrownew ConcurrentModificationException(); } while (i != size && modCount == expectedModCount) { consumer.accept((E) elementData[offset + (i++)]); } // update once at end of iteration to reduce heap write traffic lastRet = cursor = i; checkForComodification(); }
publicintnextIndex(){ return cursor; }
publicintpreviousIndex(){ return cursor - 1; }
publicvoidremove(){ if (lastRet < 0) thrownew IllegalStateException(); checkForComodification();