美文网首页
集合List添加集合,复制内容的方法

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

作者: qiwx | 来源:发表于2017-12-28 20:25 被阅读0次

第一种: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 = c.toArray();

    if ((size = elementData.length) != 0) {

        // c.toArray might (incorrectly) not return Object[] (see 6260652)

        if (elementData.getClass() != Object[].class)

            elementData = Arrays.copyOf(elementData, size, Object[].class);//核心代码

    } else {

        // replace with empty array.

        this.elementData = EMPTY_ELEMENTDATA;

    }

}

相关文章

网友评论

      本文标题:集合List添加集合,复制内容的方法

      本文链接:https://www.haomeiwen.com/subject/mptbgxtx.html