/** Snapshot of the array */private final Object[] snapshot;
源码分析
jdk1.7.0_71
123456
//锁,保护所有存取器transient final ReentrantLock lock = new ReentrantLock();//保存数据的数组private volatile transient Object[] array;final Object[] getArray() {return array;}final void setArray(Object[] a) {array = a;}
空构造,初始化一个长度为0的数组
123
public CopyOnWriteArrayList() { setArray(new Object[0]); }
利用集合初始化一个CopyOnWriteArrayList
1
public CopyOnWriteArrayList(Collection<? extends E> c) {}
利用数组初始化一个CopyOnWriteArrayList
1
public CopyOnWriteArrayList(E[] toCopyIn) {}
size() 大小
1
public int size() {}
isEmpty()是否为空
1
public boolean isEmpty(){}
indexOf(Object o, Object[] elements,int index, int fence) 元素索引
1
private static int indexOf(Object o, Object[] elements,int index, int fence) {}
indexOf() 元素索引
1
public int indexOf(Object o){}
indexOf(E e, int index) 元素索引
1
public int indexOf(E e, int index) {}
lastIndexOf(Object o, Object[] elements, int index) 元素索引,最后一个
1
private static int lastIndexOf(Object o, Object[] elements, int index) {}
lastIndexOf(Object o) 元素索引,最后一个
1
public int indexOf(E e, int index) {}
lastIndexOf(E e, int index) 元素索引,最后一个
1
public int lastIndexOf(E e, int index) {}
contains(Object o) 是否包含元素
1
public boolean contains(Object o){}
clone() 浅拷贝
1
public Object clone() {}
toArray() 转换成数组
1
public Object[] toArray(){}
toArray(T a[]) 转换成指定类型的数组
1
public <T> T[] toArray(T a[]) {}
E get(int index)获取指定位置的元素
1
public E get(int index){}
set(int index, E element) 指定位置设置元素
写元素的时候,先获得锁,finall块中释放锁
123456789101112131415161718192021
public E set(int index, E element) { final ReentrantLock lock = this.lock; lock.lock(); try { Object[] elements = getArray(); E oldValue = get(elements, index);
if (oldValue != element) { int len = elements.length; Object[] newElements = Arrays.copyOf(elements, len); newElements[index] = element; setArray(newElements); } else { // Not quite a no-op; ensures volatile write semantics setArray(elements); } return oldValue; } finally { lock.unlock(); } }
add(E e) 元素添加到末尾
1
public boolean add(E e) {}
add(int index, E element) 指定位置之后插入元素
1
public void add(int index, E element){}
remove(int index)删除指定位置的元素
1
public E remove(int index) {}
remove(Object o) 删除第一个匹配的元素
1
public boolean remove(Object o) {}
removeRange(int fromIndex, int toIndex) 删除指定区间的元素
1
private void removeRange(int fromIndex, int toIndex) {}
addIfAbsent(E e) 如果元素不存在就添加进list中
1
public boolean addIfAbsent(E e){}
containsAll(Collection<?> c)是否包含全部
1
public boolean containsAll(Collection<?> c){}
removeAll(Collection<?> c) 移除全部包含在集合中的元素
1
public boolean removeAll(Collection<?> c){}
retainAll(Collection<?> c) 保留指定集合的元素,其他的删除
1
public boolean retainAll(Collection<?> c){}
addAllAbsent(Collection<? extends E> c) 如果不存在就添加进去
1
public int addAllAbsent(Collection<? extends E> c) {}
clear() 清空list
1
public void clear(){}
addAll(Collection<? extends E> c)添加集合中的元素到尾部
1
public void addAll(Collection<? extends E> c){}
addAll(int index, Collection<? extends E> c) 添加集合中元素到指定位置之后
1
public boolean addAll(int index, Collection<? extends E> c){}
toString()
1
public String toString(){}
equals(Object o)
1234567891011121314151617
public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof List)) return false;
List<?> list = (List<?>)(o); Iterator<?> it = list.iterator(); Object[] elements = getArray(); int len = elements.length; for (int i = 0; i < len; ++i) if (!it.hasNext() || !eq(elements[i], it.next())) return false; if (it.hasNext()) return false; return true; }
hashCode()
1
public int hashCode{}
listIterator(final int index)和 listIterator() 返回一个迭代器,支持向前和向后遍历
123
public ListIterator<E> listIterator(final int index) {}
public ListIterator<E> listIterator() {}
iterator() 只能向后遍历
1
public Iterator<E> iterator() {}
subList() 返回部分list
12345
public List<E> subList(int fromIndex, int toIndex) { ... return new COWSubList<E>(this, fromIndex, toIndex); ...}