07-how-to-copy-list
7. Copy ArrayList的四种方式
简介
使用构造函数
public ArrayList(Collection<? extends E> 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;
}
}使用addAll方法
使用Collections.copy
使用stream
总结
最后更新于
这有帮助吗?