ArrayList
JDK版本:java11
fail-fast
,快速失败机制,当对ArrayList进行迭代的时候,如果多个线程进行操作的时候,就会抛出ConcurrentModificationException
当前的操作次数与数组的操作次数进行比较,来判断是否有其他线程对当前对象进行修改
clear()和removeAll()
操作次数加1,设置size=0,将数组中保存的元素置空
Removes from this list all of its elements that are contained in the
specified collection.
public boolean removeAll(Collection<?> c) {
return batchRemove(c, false, 0, size);
}
移除列表中所有包含在指定集合中的元素
removeAll(Collection<?> c)中调用的的是批量删除的方法,即batchRemove(c, false, 0, size);
扩容
Returns a capacity at least as large as the given minimum capacity. Returns the current capacity increased by 50% if that suffices. Will not return a capacity greater than MAX_ARRAY_SIZE unless the given minimum capacity is greater than MAX_ARRAY_SIZE.返回至少和给定的最小容量一样大的存储大小,一般是1.5倍大小,不会返回容量大于最大值
添加
System.arraycopy(elementData, index, elementData, index+1, s-index)
Params:参数说明
src – the source array. 原数组(被复制的数组)
srcPos – starting position in the source array. 需要被复制元素的起始位置
dest – the destination array. 目标数组
destPos – starting position in the destination data. 复制元素添加的位置
length – the number of array elements to be copied. 复制元素的个数
addAll(Collection<? extends E> c)和addAll(int inedex,Collection<? extends E> c)与与上相同,只是将指定集合中的元素添加到数组中