/**
* 将列表中的对象全部置为null,但是列表的容量不会改变
*/
public void clear() {
modCount++;
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
/**
* 将收集器c中的对象全部加入当前列表中
*/
public boolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew); // 尝试扩容
System.arraycopy(a, 0, elementData, size, numNew);
size += numNew;
return numNew != 0;
}
/**
* 将序列c中的元素插入到列表中,插入的位置由传入的index决定
*/
public boolean addAll(int index, Collection<? extends E> c) {
rangeCheckForAdd(index);
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew); // 扩容
int numMoved = size - index;
if (numMoved > 0)
System.arraycopy(elementData, index, elementData, index + numNew,
numMoved);
System.arraycopy(a, 0, elementData, index, numNew);
size += numNew;
return numNew != 0;
}
/**
* 删除当前列表中从fromIndex开始到toIndex的元素
*/
protected void removeRange(int fromIndex, int toIndex) {
modCount++;
int numMoved = size - toIndex; //需要向前移动的元素数量
System.arraycopy(elementData, toIndex, elementData, fromIndex,
numMoved);
int newSize = size - (toIndex-fromIndex); //需要置为null的初始位置
for (int i = newSize; i < size; i++) {
elementData[i] = null;
}
size = newSize;
}
/**
* 检查位置参数index是否合法
*/
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
/**
* 检查位置参数index是否合法,这个参数一般用来向序列添加元素
*/
private void rangeCheckForAdd(int index) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
/**
* 位置不合法的提示字符串
*/
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size;
}
/**
* 移除当前列表所有存在于集合c中的yuan
*/
public boolean removeAll(Collection<?> c) {
Objects.requireNonNull(c);
return batchRemove(c, false);
}
/**
* 移除当前列表所有不存在于集合c中的元素
*/
public boolean retainAll(Collection<?> c) {
Objects.requireNonNull(c);
return batchRemove(c, true);
}
/**
* 根据传递的complement的值移除元素
* 如果complement为true则移除不存在与集合中的元素,如果为false移除存在于集合中的元素
*/
private boolean batchRemove(Collection<?> c, boolean complement) {
final Object[] elementData = this.elementData;
int r = 0, w = 0;
boolean modified = false;
try {
//对列表进行遍历,当complement为true的时候,所有存在于集合c中的元素保留下来
//当complement为false的时候,所有不存在与集合c中的元素保留下来
for (; r < size; r++)
if (c.contains(elementData[r]) == complement)
elementData[w++] = elementData[r];
} finally {
// 如果在遍历过程中抛出了异常,则将抛出异常位置之后的所有元素全部保留
if (r != size) {
System.arraycopy(elementData, r,
elementData, w,
size - r);
w += size - r;
}
if (w != size) {
// 将没有保留下来的元素置为null
for (int i = w; i < size; i++)
elementData[i] = null;
modCount += size - w;
size = w;
modified = true;
}
}
return modified;
}
/**
* 序列化当前列表,即保存当前列表为一个流
*/
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException{
// 记录列表的当前被修改次数
int expectedModCount = modCount;
s.defaultWriteObject();
// 序列化列表的大小
s.writeInt(size);
// 序列化列表的每一个元素
for (int i=0; i<size; i++) {
s.writeObject(elementData[i]);
}
//如果最后发现记录的开始修改次数与最后执行完方法的列表修改次数不一致
//说明在序列化的过程中有其他的线程对当前列表进行了修改(ArrayList非线程安全),抛出异常
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
/**
* 从流中反序列化一个ArrayList实例
*/
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
elementData = EMPTY_ELEMENTDATA;//将当前列表的值,置为默认空的状态
s.defaultReadObject();
// 读取列表的大小
s.readInt();
if (size > 0) {
// 对当前列表进行扩容
ensureCapacityInternal(size);
Object[] a = elementData;
// 将流中的列表元素反序列化,并存储到当前列表的值数组中
for (int i=0; i<size; i++) {
a[i] = s.readObject();
}
}
}
/**
* 从列表中的指定位置开始,返回此列表中的列表迭代器
*/
public ListIterator<E> listIterator(int index) {
if (index < 0 || index > size)
throw new IndexOutOfBoundsException("Index: "+index);
return new ListItr(index);
}
/**
* 返回此列表中从0开始的列表迭代器
*/
public ListIterator<E> listIterator() {
return new ListItr(0);
}
/**
* 返回列表中元素的迭代器
*/
public Iterator<E> iterator() {
return new Itr();
}
/**
* 迭代器的类
*/
private class Itr implements Iterator<E> {
int cursor; // 迭代器句柄所在位置
int lastRet = -1; // 最后一个元素的索引,如果列表是空的,返回-1
int expectedModCount = modCount;
//检查是否还有下一个元素
public boolean hasNext() {
return cursor != size;
}
//获取dangqian
@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];
}
//移除当前迭代器所在位置的前一个元素
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();//检查当前列表是否被其他线程修改过
try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
//这是Java 8为Iterator新增的默认方法,该方法可使用Lambda表达式来遍历集合元素
@Override
@SuppressWarnings("unchecked")
public void forEachRemaining(Consumer<? super E> consumer) {
Objects.requireNonNull(consumer);
final int size = ArrayList.this.size;
int i = cursor;
if (i >= size) {
return;
}
final Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length) {
throw new ConcurrentModificationException();
}
while (i != size && modCount == expectedModCount) {
consumer.accept((E) elementData[i++]);
}
// 在迭代结束时更新一次句柄的位置,减少对堆中对象的访问次数
cursor = i;
lastRet = i - 1;
checkForComodification();
}
//检查当前列表是否被其他线程修改过
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
Java源码阅读之ArrayList(3)
最新推荐文章于 2022-08-19 18:25:37 发布