// 按照位置删除索引public E remove(int index){// 检查当前位置是否合法rangeCheck(index);// 操作数+1
modCount++;// 旧值
E oldValue =elementData(index);// 需要移动的数量int numMoved = size - index -1;if(numMoved >0)// 将index + 1开始的数据统一前移一位,及index的位置
System.arraycopy(elementData, index+1, elementData, index,
numMoved);// 将最后一位置null,size-1
elementData[--size]= null;// clear to let GC do its work// 返回被删除的值return oldValue;}// 按照值删除publicbooleanremove(Object o){// o 为 null 的情况if(o == null){// 遍历for(int index =0; index < size; index++)if(elementData[index]== null){// 快速删除fastRemove(index);returntrue;}// o 不为 null 的情况 }else{// 遍历for(int index =0; index < size; index++)if(o.equals(elementData[index])){// 快速删除fastRemove(index);returntrue;}}returnfalse;}// 跳过边界检查且不返回值的删除方法privatevoidfastRemove(int index){// 操作次数+1
modCount++;// 要移动的元素个数int numMoved = size - index -1;if(numMoved >0)// 将index+1开始的numMoved个数的元素,移动到index开始
System.arraycopy(elementData, index+1, elementData, index,
numMoved);// 将最后一位置null,size-1
elementData[--size]= null;// clear to let GC do its work}
6、删除多个元素
// 批量移除 [fromIndex, toIndex) 的多个元素,左闭右开protectedvoidremoveRange(int fromIndex,int toIndex){// 操作次数+1
modCount++;// 要移动的元素个数int numMoved = size - toIndex;// 将toIndex及之后的numMoved个元素,移动至formIndex开始
System.arraycopy(elementData, toIndex, elementData, fromIndex,
numMoved);// clear to let GC do its work// 更新sizeint newSize = size -(toIndex-fromIndex);// 将删除掉的元素全部置nullfor(int i = newSize; i < size; i++){
elementData[i]= null;}// 返回删除元素的个数
size = newSize;}
// 批量移除指定的多个元素publicbooleanremoveAll(Collection<?> c){// 集合判空校验
Objects.requireNonNull(c);// 批量删除returnbatchRemove(c,false);}// 批量删除privatebooleanbatchRemove(Collection<?> c,boolean complement){final Object[] elementData =this.elementData;int r =0, w =0;boolean modified =false;try{// 遍历for(; r < size; r++)// 如果集合中没有该元素if(c.contains(elementData[r])== complement)// 将不符合元素重头覆盖
elementData[w++]= elementData[r];}finally{// Preserve behavioral compatibility with AbstractCollection,// even if c.contains() throws.// 当r和size不相等的时候(如果 contains 方法发生异常,则将剩下的数据从 r 位置的数据写入到从 w 开始的位置)if(r != size){// 将r及之后的元素,复制到w及之后
System.arraycopy(elementData, r,
elementData, w,
size - r);// w + (size - r)
w += size - r;}// 当w和size不相等的时候if(w != size){// clear to let GC do its work//将w及之后的数据全置空for(int i = w; i < size; i++)
elementData[i]= null;//操作次数
modCount += size - w;// size改为w
size = w;
modified =true;}}return modified;}// 清空数组publicvoidclear(){// 操作数+1
modCount++;// clear to let GC do its work// for循环置空for(int i =0; i < size; i++)
elementData[i]= null;
size =0;}
7、查询操作
// 根据下标获取元素public E get(int index){// 检查当前位置是否合法 rangeCheck(index);// 返回returnelementData(index);}// 根据元素获取下标,遍历获取(获取第一次出现的下标)publicintindexOf(Object o){if(o == null){for(int i =0; i < size; i++)if(elementData[i]==null)return i;}else{for(int i =0; i < size; i++)if(o.equals(elementData[i]))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;}// 设置指定下边的值,并返回旧值public E set(int index, E element){// 检查当前位置是否合法 rangeCheck(index);
E oldValue =elementData(index);
elementData[index]= element;return oldValue;}
8、迭代器Iterator
//arrayList.iterator()public Iterator<E>iterator(){returnnewItr();}// 内部类AbstractList.Itr 的优化版本privateclassItrimplementsIterator<E>{// 下一次访问元素的位置int cursor;// index of next element to return// 上一次访问元素的位置; 如果没有就返回-1int lastRet =-1;// index of last element returned; -1 if no such// 创建迭代器时,数组修改次数,如果这个过程中数组修改了 就会跑异常int expectedModCount = modCount;Itr(){}// 判断是否还可以继续迭代publicbooleanhasNext(){// 判断下一次访问位置等于size吗return cursor != size;}// 获取下一个元素@SuppressWarnings("unchecked")public E next(){// 校验是否数组发生了变化checkForComodification();int i = cursor;// 判断如果超过 size 范围,抛出 NoSuchElementException 异常if(i >= size)thrownewNoSuchElementException();
Object[] elementData = ArrayList.this.elementData;// 判断如果超过 elementData 大小,说明可能被修改了,抛出 ConcurrentModificationException 异常if(i >= elementData.length)thrownewConcurrentModificationException();// 指向下一个位置
cursor = i +1;// 返回当前位置的元素,lastRet指向当前位置return(E) elementData[lastRet = i];}// 移除当前元素publicvoidremove(){// 如果 lastRet 小于 0 ,说明没有指向任何元素,抛出 IllegalStateException 异常if(lastRet <0)thrownewIllegalStateException();// 校验是否数组发生了变化checkForComodification();try{// 移除 lastRet 位置的元素
ArrayList.this.remove(lastRet);// cursor 指向 lastRet 位置,因为被移了,所以需要后退下
cursor = lastRet;// lastRet 标记为 -1 ,因为当前元素被移除了
lastRet =-1;// 更新数组的修改次数
expectedModCount = modCount;}catch(IndexOutOfBoundsException ex){thrownewConcurrentModificationException();}}// 就是对集合中剩余的元素进行操作,直到元素完毕或者抛出异常。这里重要的是剩余元素@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){thrownewConcurrentModificationException();}// 当有没有遍历的值且数组没有被修改过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)thrownewConcurrentModificationException();}}