clear()刪除錯有緩衝區裏的數據java
public void clear() { modCount++; final Object[] es = elementData; for (int to = size, i = size = 0; i < to; i++)//實際存儲數據置0,從0到實際存儲的位置循環置null es[i] = null; }
addAll(Collection<? extends E> c)添加集合到當前集合數組
public boolean addAll(Collection<? extends E> c) { Object[] a = c.toArray();//轉化爲數組 modCount++; int numNew = a.length;//添加數據長度 if (numNew == 0) return false;//長度爲0直接返回false Object[] elementData; final int s; if (numNew > (elementData = this.elementData).length - (s = size))//舊數據長度+新數據長度大於緩衝區大小,就擴容 elementData = grow(s + numNew);//擴大爲能夠容納舊數據+新數據大小 System.arraycopy(a, 0, elementData, s, numNew);//新數據從0位開始複製到緩衝區的s位處,複製長度爲新數據長度 size = s + numNew; return true; }
addAll(int index, Collection<? extends E> c)添加集合到當前集合的固定位置安全
public boolean addAll(int index, Collection<? extends E> c) { rangeCheckForAdd(index);//確認下標 Object[] a = c.toArray();//轉數組 modCount++; int numNew = a.length; if (numNew == 0) return false;//長度爲0直接返回 Object[] elementData; final int s; if (numNew > (elementData = this.elementData).length - (s = size))//舊數據長度+新數據長度大於緩衝區大小,就擴容 elementData = grow(s + numNew); int numMoved = s - index;//存儲長度減去index得出就是要移動數據的長度 if (numMoved > 0) System.arraycopy(elementData, index,elementData, index + numNew,numMoved);//把緩衝區從index移動到index + numNew,移動長度爲numMoved System.arraycopy(a, 0, elementData, index, numNew);//把集合從0位移動到緩衝區index位,共移動集合的長度個數據 size = s + numNew;//實際存儲數更改成size+集合長度 return true;//返回true }
removeRange(int fromIndex, int toIndex)刪除介於(包含)fromIndex和toIndex(不包含)的全部元素app
protected void removeRange(int fromIndex, int toIndex) { if (fromIndex > toIndex) { throw new IndexOutOfBoundsException( outOfBoundsMsg(fromIndex, toIndex)); } modCount++; shiftTailOverGap(elementData, fromIndex, toIndex); }
shiftTailOverGap(Object[] es, int lo, int hi)刪除lo(包含)到hi(不包含)期間的元素less
private void shiftTailOverGap(Object[] es, int lo, int hi) { System.arraycopy(es, hi, es, lo, size - hi);//從hi位之後的數據複製到lo位,共複製size-hi個數據 for (int to = size, i = (size -= hi - lo); i < to; i++) es[i] = null;//置0 }
rangeCheckForAdd(int index)判斷是否在區間內dom
private void rangeCheckForAdd(int index) { if (index > size || index < 0) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); }
下標越界消息ide
private String outOfBoundsMsg(int index) { return "Index: "+index+", Size: "+size; } private static String outOfBoundsMsg(int fromIndex, int toIndex) { return "From Index: " + fromIndex + " > To Index: " + toIndex; }
removeAll(Collection<?> c) 刪除緩衝區中,集合包含的數據oop
public boolean removeAll(Collection<?> c) { return batchRemove(c, false, 0, size); }
retainAll(Collection<?> c)保留緩衝區中,集合包含的數據ui
public boolean retainAll(Collection<?> c) { return batchRemove(c, true, 0, size); }
batchRemove(Collection<?> c, boolean complement,final int from, final int end)false是刪除傳入集合包含元素,true是保留傳入集合包含元素this
boolean batchRemove(Collection<?> c, boolean complement, final int from, final int end) { Objects.requireNonNull(c); final Object[] es = elementData; int r; // Optimize for initial run of survivors for (r = from;; r++) { if (r == end)//操做長度爲0直接返回false return false; if (c.contains(es[r]) != complement)//爲true的時候,查找到第一個不保留位r。爲false時候查找到第一個要刪除的位 break; } int w = r++; try { for (Object e; r < end; r++) if (c.contains(e = es[r]) == complement)//爲true時把在集合的元素往前移,爲false時,不在集合的元素往前移動 es[w++] = e; } catch (Throwable ex) { // Preserve behavioral compatibility with AbstractCollection, // even if c.contains() throws. System.arraycopy(es, r, es, w, end - r); w += end - r; throw ex; } finally { modCount += end - w; shiftTailOverGap(es, w, end);//刪除尾部元素 } return true; }
writeObject(java.io.ObjectOutputStream s)輸出對象
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { // Write out element count, and any hidden stuff int expectedModCount = modCount; s.defaultWriteObject(); // Write out size as capacity for behavioral compatibility with clone() s.writeInt(size); // Write out all elements in the proper order. for (int i=0; i<size; i++) { s.writeObject(elementData[i]);//循環輸出對象 } if (modCount != expectedModCount) { throw new ConcurrentModificationException();//線程安全 } }
readObject(java.io.ObjectInputStream s)讀取對象
private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { // Read in size, and any hidden stuff s.defaultReadObject(); // Read in capacity s.readInt(); // ignored if (size > 0) {//數據量大於0 // like clone(), allocate array based upon size not capacity SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, size); Object[] elements = new Object[size]; // Read in all elements in the proper order. for (int i = 0; i < size; i++) { elements[i] = s.readObject(); } elementData = elements; } else if (size == 0) {//數據量等於0 elementData = EMPTY_ELEMENTDATA; } else { throw new java.io.InvalidObjectException("Invalid size: " + size); } }
listIterator()返回迭代器
public ListIterator<E> listIterator() { return new ListItr(0); }
listIterator(int index)返回迭代器
public ListIterator<E> listIterator(int index) { rangeCheckForAdd(index);// return new ListItr(index); }
private class Itr implements Iterator<E> { int cursor; // 要返回的下一個元素的索引 int lastRet = -1; // 返回最後一個元素的索引; 若是沒有這樣的話-1 int expectedModCount = modCount; // prevent creating a synthetic constructor Itr() {} public boolean hasNext() { return cursor != size; } @SuppressWarnings("unchecked") public E next() { checkForComodification();//線程安全 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];//最後一個元素的索引改爲i } public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { ArrayList.this.remove(lastRet);//移除最後返回的元素 cursor = lastRet;//光標回退 lastRet = -1;//最後返回的元素被刪除,索引變爲-1 expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } @Override public void forEachRemaining(Consumer<? super E> action) {//循環剩餘 Objects.requireNonNull(action); final int size = ArrayList.this.size; int i = cursor; if (i < size) {// final Object[] es = elementData; if (i >= es.length) throw new ConcurrentModificationException();//線程異常 for (; i < size && modCount == expectedModCount; i++) action.accept(elementAt(es, i));//把緩衝區es中i處元素放進accept方法裏 // update once at end to reduce heap write traffic cursor = i; lastRet = i - 1; checkForComodification(); } } final void checkForComodification() {//線程安全 if (modCount != expectedModCount) throw new ConcurrentModificationException(); } }
subList(int fromIndex, int toIndex)返回集合的部分(類型變成了SubList)
public List<E> subList(int fromIndex, int toIndex) { subListRangeCheck(fromIndex, toIndex, size); return new SubList<>(this, fromIndex, toIndex); }
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; } @SuppressWarnings("unchecked") 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[lastRet = i]; } public void set(E e) { if (lastRet < 0)//最後操做位必須大於0,即進行刪除操做後得滑動索引,否則會報IllegalStateException throw new IllegalStateException(); checkForComodification(); try { ArrayList.this.set(lastRet, e);//最後操做位處插入 } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } public void add(E e) { checkForComodification();//線程安全 try { int i = cursor; ArrayList.this.add(i, e);//在下一個操做位處添加元素 cursor = i + 1;//後移光標 lastRet = -1;//清空最後操做元素 expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } }
subList(int fromIndex, int toIndex)
public List<E> subList(int fromIndex, int toIndex) { subListRangeCheck(fromIndex, toIndex, size); return new SubList<>(this, fromIndex, toIndex); }
private static class SubList<E> extends AbstractList<E> implements RandomAccess { private final ArrayList<E> root; private final SubList<E> parent; private final int offset; private int size; /** * Constructs a sublist of an arbitrary ArrayList. */ public SubList(ArrayList<E> root, int fromIndex, int toIndex) { this.root = root; this.parent = null; this.offset = fromIndex; this.size = toIndex - fromIndex; this.modCount = root.modCount; } /** * Constructs a sublist of another SubList. */ private SubList(SubList<E> parent, int fromIndex, int toIndex) { this.root = parent.root; this.parent = parent; this.offset = parent.offset + fromIndex; this.size = toIndex - fromIndex; this.modCount = root.modCount; } public E set(int index, E element) { Objects.checkIndex(index, size); checkForComodification(); E oldValue = root.elementData(offset + index); root.elementData[offset + index] = element; return oldValue; } public E get(int index) { Objects.checkIndex(index, size); checkForComodification(); return root.elementData(offset + index); } public int size() { checkForComodification(); return size; } public void add(int index, E element) { rangeCheckForAdd(index); checkForComodification(); root.add(offset + index, element); updateSizeAndModCount(1); } public E remove(int index) { Objects.checkIndex(index, size); checkForComodification(); E result = root.remove(offset + index); updateSizeAndModCount(-1); return result; } protected void removeRange(int fromIndex, int toIndex) { checkForComodification(); root.removeRange(offset + fromIndex, offset + toIndex); updateSizeAndModCount(fromIndex - toIndex); } 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(); root.addAll(offset + index, c); updateSizeAndModCount(cSize); return true; } public void replaceAll(UnaryOperator<E> operator) { root.replaceAllRange(operator, offset, offset + size); } public boolean removeAll(Collection<?> c) { return batchRemove(c, false); } public boolean retainAll(Collection<?> c) { return batchRemove(c, true); } private boolean batchRemove(Collection<?> c, boolean complement) { checkForComodification(); int oldSize = root.size; boolean modified = root.batchRemove(c, complement, offset, offset + size); if (modified) updateSizeAndModCount(root.size - oldSize); return modified; } public boolean removeIf(Predicate<? super E> filter) { checkForComodification(); int oldSize = root.size; boolean modified = root.removeIf(filter, offset, offset + size); if (modified) updateSizeAndModCount(root.size - oldSize); return modified; } public Object[] toArray() { checkForComodification(); return Arrays.copyOfRange(root.elementData, offset, offset + size); } @SuppressWarnings("unchecked") public <T> T[] toArray(T[] a) { checkForComodification(); if (a.length < size) return (T[]) Arrays.copyOfRange( root.elementData, offset, offset + size, a.getClass()); System.arraycopy(root.elementData, offset, a, 0, size); if (a.length > size) a[size] = null; return a; } public boolean equals(Object o) { if (o == this) { return true; } if (!(o instanceof List)) { return false; } boolean equal = root.equalsRange((List<?>)o, offset, offset + size); checkForComodification(); return equal; } public int hashCode() { int hash = root.hashCodeRange(offset, offset + size); checkForComodification(); return hash; } public int indexOf(Object o) { int index = root.indexOfRange(o, offset, offset + size); checkForComodification(); return index >= 0 ? index - offset : -1; } public int lastIndexOf(Object o) { int index = root.lastIndexOfRange(o, offset, offset + size); checkForComodification(); return index >= 0 ? index - offset : -1; } public boolean contains(Object o) { return indexOf(o) >= 0; } public Iterator<E> iterator() { return listIterator(); } public ListIterator<E> listIterator(int index) { checkForComodification(); rangeCheckForAdd(index); return new ListIterator<E>() { int cursor = index; int lastRet = -1; int expectedModCount = root.modCount; public boolean hasNext() { return cursor != SubList.this.size; } @SuppressWarnings("unchecked") public E next() { checkForComodification(); int i = cursor; if (i >= SubList.this.size) throw new NoSuchElementException(); Object[] elementData = root.elementData; if (offset + i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (E) elementData[offset + (lastRet = i)]; } public boolean hasPrevious() { return cursor != 0; } @SuppressWarnings("unchecked") public E previous() { checkForComodification(); int i = cursor - 1; if (i < 0) throw new NoSuchElementException(); Object[] elementData = root.elementData; if (offset + i >= elementData.length) throw new ConcurrentModificationException(); cursor = i; return (E) elementData[offset + (lastRet = i)]; } public void forEachRemaining(Consumer<? super E> action) { Objects.requireNonNull(action); final int size = SubList.this.size; int i = cursor; if (i < size) { final Object[] es = root.elementData; if (offset + i >= es.length) throw new ConcurrentModificationException(); for (; i < size && modCount == expectedModCount; i++) action.accept(elementAt(es, offset + i)); // update once at end to reduce heap write traffic cursor = i; lastRet = i - 1; checkForComodification(); } } public int nextIndex() { return cursor; } public int previousIndex() { return cursor - 1; } public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { SubList.this.remove(lastRet); cursor = lastRet; lastRet = -1; expectedModCount = root.modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } public void set(E e) { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { root.set(offset + lastRet, e); } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } public void add(E e) { checkForComodification(); try { int i = cursor; SubList.this.add(i, e); cursor = i + 1; lastRet = -1; expectedModCount = root.modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } } final void checkForComodification() { if (root.modCount != expectedModCount) throw new ConcurrentModificationException(); } }; } public List<E> subList(int fromIndex, int toIndex) { subListRangeCheck(fromIndex, toIndex, size); return new SubList<>(this, fromIndex, toIndex); } private void rangeCheckForAdd(int index) { if (index < 0 || index > this.size) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } private String outOfBoundsMsg(int index) { return "Index: "+index+", Size: "+this.size; } private void checkForComodification() { if (root.modCount != modCount) throw new ConcurrentModificationException(); } private void updateSizeAndModCount(int sizeChange) { SubList<E> slist = this; do { slist.size += sizeChange; slist.modCount = root.modCount; slist = slist.parent; } while (slist != null); } public Spliterator<E> spliterator() { checkForComodification(); // ArrayListSpliterator not used here due to late-binding return new Spliterator<E>() { private int index = offset; // current index, modified on advance/split private int fence = -1; // -1 until used; then one past last index private int expectedModCount; // initialized when fence set private int getFence() { // initialize fence to size on first use int hi; // (a specialized variant appears in method forEach) if ((hi = fence) < 0) { expectedModCount = modCount; hi = fence = offset + size; } return hi; } public ArrayList<E>.ArrayListSpliterator trySplit() { int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; // ArrayListSpliterator can be used here as the source is already bound return (lo >= mid) ? null : // divide range in half unless too small root.new ArrayListSpliterator(lo, index = mid, expectedModCount); } public boolean tryAdvance(Consumer<? super E> action) { Objects.requireNonNull(action); int hi = getFence(), i = index; if (i < hi) { index = i + 1; @SuppressWarnings("unchecked") E e = (E)root.elementData[i]; action.accept(e); if (root.modCount != expectedModCount) throw new ConcurrentModificationException(); return true; } return false; } public void forEachRemaining(Consumer<? super E> action) { Objects.requireNonNull(action); int i, hi, mc; // hoist accesses and checks from loop ArrayList<E> lst = root; Object[] a; if ((a = lst.elementData) != null) { if ((hi = fence) < 0) { mc = modCount; hi = offset + size; } else mc = expectedModCount; if ((i = index) >= 0 && (index = hi) <= a.length) { for (; i < hi; ++i) { @SuppressWarnings("unchecked") E e = (E) a[i]; action.accept(e); } if (lst.modCount == mc) return; } } throw new ConcurrentModificationException(); } public long estimateSize() { return getFence() - index; } public int characteristics() { return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED; } }; } }
forEach(Consumer<? super E> action)迭代元素
@Override public void forEach(Consumer<? super E> action) { Objects.requireNonNull(action); final int expectedModCount = modCount; final Object[] es = elementData;//緩衝區 final int size = this.size; for (int i = 0; modCount == expectedModCount && i < size; i++)//循環0到實際長度 action.accept(elementAt(es, i));//對應下標值放入accept方法 if (modCount != expectedModCount) throw new ConcurrentModificationException();//線程安全 }
spliterator() 返回分裂器
@Override public Spliterator<E> spliterator() { return new ArrayListSpliterator(0, -1, 0); }
final class ArrayListSpliterator implements Spliterator<E> { /* * If ArrayLists were immutable, or structurally immutable (no * adds, removes, etc), we could implement their spliterators * with Arrays.spliterator. Instead we detect as much * interference during traversal as practical without * sacrificing much performance. We rely primarily on * modCounts. These are not guaranteed to detect concurrency * violations, and are sometimes overly conservative about * within-thread interference, but detect enough problems to * be worthwhile in practice. To carry this out, we (1) lazily * initialize fence and expectedModCount until the latest * point that we need to commit to the state we are checking * against; thus improving precision. (This doesn't apply to * SubLists, that create spliterators with current non-lazy * values). (2) We perform only a single * ConcurrentModificationException check at the end of forEach * (the most performance-sensitive method). When using forEach * (as opposed to iterators), we can normally only detect * interference after actions, not before. Further * CME-triggering checks apply to all other possible * violations of assumptions for example null or too-small * elementData array given its size(), that could only have * occurred due to interference. This allows the inner loop * of forEach to run without any further checks, and * simplifies lambda-resolution. While this does entail a * number of checks, note that in the common case of * list.stream().forEach(a), no checks or other computation * occur anywhere other than inside forEach itself. The other * less-often-used methods cannot take advantage of most of * these streamlinings. */ private int index; // 當前指數,在提早/拆分時修改 private int fence; // -1直到使用; 而後是最後一個索引 private int expectedModCount; // 柵欄設置時初始化 /** 建立覆蓋給定範圍的新分裂器. */ ArrayListSpliterator(int origin, int fence, int expectedModCount) { this.index = origin; this.fence = fence; this.expectedModCount = expectedModCount; } private int getFence() { // initialize fence to size on first use int hi; // (a specialized variant appears in method forEach) if ((hi = fence) < 0) { expectedModCount = modCount; hi = fence = size; } return hi; } public ArrayListSpliterator trySplit() {//拆分 int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; return (lo >= mid) ? null : // divide range in half unless too small new ArrayListSpliterator(lo, index = mid, expectedModCount); } public boolean tryAdvance(Consumer<? super E> action) {//迭代,如有下一位返回true if (action == null) throw new NullPointerException(); int hi = getFence(), i = index; if (i < hi) { index = i + 1; @SuppressWarnings("unchecked") E e = (E)elementData[i]; action.accept(e); if (modCount != expectedModCount) throw new ConcurrentModificationException(); return true; } return false; } public void forEachRemaining(Consumer<? super E> action) { int i, hi, mc; // hoist accesses and checks from loop Object[] a; if (action == null) throw new NullPointerException(); if ((a = elementData) != null) { if ((hi = fence) < 0) { mc = modCount; hi = size; } else mc = expectedModCount; if ((i = index) >= 0 && (index = hi) <= a.length) { for (; i < hi; ++i) { @SuppressWarnings("unchecked") E e = (E) a[i]; action.accept(e); } if (modCount == mc) return; } } throw new ConcurrentModificationException(); } public long estimateSize() { return getFence() - index; } public int characteristics() { return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED; } }
removeIf(Predicate<? super E> filter) 刪除表達式返回true的元素
@Override public boolean removeIf(Predicate<? super E> filter) { return removeIf(filter, 0, size); }
removeIf(Predicate<? super E> filter, int i, final int end)刪除範圍呃逆,表達式返回true的元素
boolean removeIf(Predicate<? super E> filter, int i, final int end) { Objects.requireNonNull(filter); int expectedModCount = modCount; final Object[] es = elementData;//緩衝區 // Optimize for initial run of survivors for (; i < end && !filter.test(elementAt(es, i)); i++) ; // Tolerate predicates that reentrantly access the collection for // read (but writers still get CME), so traverse once to find // elements to delete, a second pass to physically expunge. if (i < end) { final int beg = i; final long[] deathRow = nBits(end - beg); deathRow[0] = 1L; // set bit 0 for (i = beg + 1; i < end; i++) if (filter.test(elementAt(es, i))) setBit(deathRow, i - beg); if (modCount != expectedModCount) throw new ConcurrentModificationException(); modCount++; int w = beg; for (i = beg; i < end; i++) if (isClear(deathRow, i - beg)) es[w++] = es[i]; shiftTailOverGap(es, w, end); return true; } else { if (modCount != expectedModCount) throw new ConcurrentModificationException(); return false; } }
replaceAll(UnaryOperator<E> operator)替換範圍內元素
@Override public void replaceAll(UnaryOperator<E> operator) { replaceAllRange(operator, 0, size); modCount++; }
replaceAllRange(UnaryOperator<E> operator, int i, int end)替換範圍內元素,每一個元素都替換成執行UnaryOperator後的結果
private void replaceAllRange(UnaryOperator<E> operator, int i, int end) { Objects.requireNonNull(operator); final int expectedModCount = modCount; final Object[] es = elementData; for (; modCount == expectedModCount && i < end; i++) es[i] = operator.apply(elementAt(es, i)); if (modCount != expectedModCount) throw new ConcurrentModificationException(); }
sort(Comparator<? super E> c)排序集合
public void sort(Comparator<? super E> c) { final int expectedModCount = modCount; Arrays.sort((E[]) elementData, 0, size, c);//調用Arrays.sort if (modCount != expectedModCount) throw new ConcurrentModificationException(); modCount++; }
checkInvariants() 檢查不變量
void checkInvariants() { // assert size >= 0; // assert size == elementData.length || elementData[size] == null; }