ArrayList源码(删)
源码
// 根据位置移除元素,返回删除元素 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, elementData, index, numMoved); elementData[--size] = null; // clear to let GC do its work 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; // clear to let GC do its work } // 移除指定范围的元素 protected void removeRange(int fromIndex, int toIndex) { modCount++; int numMoved = size - toIndex; System.arraycopy(elementData, toIndex, elementData, fromIndex, numMoved); // clear to let GC do its work int newSize = size - (toIndex-fromIndex); for (int i = newSize; i < size; i++) { elementData[i] = null; } size = newSize; }
// 移除所有在传入参入集合内的参数 public boolean removeAll(Collection<?> c) { Objects.requireNonNull(c); return batchRemove(c, false); } // 清空队列内元素,但队列还实际存在 public void clear() { modCount++; // clear to let GC do its work for (int i = 0; i < size; i++) elementData[i] = null; size = 0; }
相关推荐
源码物语 2020-07-18
xiaouncle 2020-07-31
XGQ 2020-06-21
fengyun 2020-06-14
OldBowl 2020-06-11
zagnix 2020-06-04
xhao 2020-06-04
付春杰Blog 2020-05-31
ilovewqf 2020-05-30
shenxiuwen 2020-05-29
XGQ 2020-05-26
zhuyonge 2020-05-09
zcpHappy 2020-04-30
luohui 2020-04-29
fraternityjava 2020-04-29
willluckysmile 2020-04-24
shayuchaor 2020-04-20
fengyun 2020-04-17