CopyOnWriteArrayList
public void add(int index, E element) { final ReentrantLock lock = this.lock; lock.lock(); try { Object[] elements = getArray(); int len = elements.length; if (index > len || index < 0) throw new IndexOutOfBoundsException("Index: "+index+ ", Size: "+len); Object[] newElements; int numMoved = len - index; if (numMoved == 0) newElements = Arrays.copyOf(elements, len + 1); //通过复制将添加的元素 添加到数组中 else { newElements = new Object[len + 1]; System.arraycopy(elements, 0, newElements, 0, index); //整体的复制,每次都 //是一个新的数组 System.arraycopy(elements, index, newElements, index + 1, numMoved); } newElements[index] = element; setArray(newElements); } finally { lock.unlock();