//插入的时候,判断位置是否合法,上下限都要判断 privatevoidrangeCheckForAdd(int index){ if (index > size || index < 0) thrownew IndexOutOfBoundsException(outOfBoundsMsg(index)); }
//删除指定位置上的元素 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; // 把原来的最后一个元素设置为null,提醒JVM GC
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 }
//指定*最小*容量 publicvoidensureCapacity(int minCapacity){ int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA) // any size if not default element table ? 0 // larger than default for default empty table. It's already // supposed to be at default size. : DEFAULT_CAPACITY;
if (minCapacity > minExpand) { ensureExplicitCapacity(minCapacity); } }
//查找元素位置,从头往后找 publicintindexOf(Object o){ if (o == null) { for (int i = 0; i < size; i++) if (elementData[i]==null) //如果是null,则直接使用==判断 return i; } else { for (int i = 0; i < size; i++) if (o.equals(elementData[i])) //如果是对象,使用equals判断 return i; } return -1; }
//查找元素位置,从后往前找 publicintlastIndexOf(Object o){ if (o == null) { for (int i = size-1; i >= 0; i--) if (elementData[i]==null) return i; } else { for (int i = size-1; i >= 0; i--) if (o.equals(elementData[i])) return i; } return -1; }
//指定范围批量删除,不包含toIndex指的元素 protectedvoidremoveRange(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; }
privatevoidreadObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { // Read in size, and any hidden stuff s.defaultReadObject();
// Read in array length and allocate array int arrayLength = s.readInt(); Object[] a = elementData = new Object[arrayLength];
// Read in all elements in the proper order. for (int i=0; i<size; i++) a[i] = s.readObject(); }
//获取从指定位置开始的List迭代器 public ListIterator<E> listIterator(int index){ if (index < 0 || index > size) thrownew IndexOutOfBoundsException("Index: "+index); returnnew ListItr(index); }
//获取包含全部元素的List迭代器 public ListIterator<E> listIterator(){ returnnew ListItr(0); }
//获取普通迭代器 public Iterator<E> iterator(){ returnnew 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;
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]; }
// 删除next中返回的元素 publicvoidremove(){ if (lastRet < 0) thrownew IllegalStateException(); checkForComodification();
@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(); }
@SuppressWarnings("unchecked") public E previous(){ checkForComodification(); 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]; }
publicvoidset(E e){ if (lastRet < 0) thrownew IllegalStateException(); checkForComodification();
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();