集合List添加集合,复制内容的方法

第一种:AddAll(): ArrayList 的AddAll()方法如下: public boolean addAll(Collection c) { Object[] a = c.toArray(); int numNew = a.length; ensureCapacityInternal(size + numNew); // Increments modCount System.arraycopy(a, 0, elementData, size, numNew); //核心代码 size += numNew; return numNew != 0; } 第二种:ArrayList(List A) public ArrayList(Collection c) { elementData = https://www.it610.com/article/c.toArray(); if ((size = elementData.length) != 0) { // c.toArray might (incorrectly) not return Object[] (see 6260652) if (elementData.getClass() != Object[].class) elementData = https://www.it610.com/article/Arrays.copyOf(elementData, size, Object[].class); //核心代码 } else { // replace with empty array. this.elementData = https://www.it610.com/article/EMPTY_ELEMENTDATA; } }

    推荐阅读