public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable{
// 长度为0的空数组
private static final Object[] EMPTY_ELEMENTDATA = {};
// 默认容量为空的数组
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
// 存储元素的数组
transient Object[] elementData;
// 数组的长度
private int size;
// 数组的默认容量为10
private static final int DEFAULT_CAPACITY = 10;
public boolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray(); // 将有数据的集合转换为数组
int numNew = a.length; // 将有数据的集合长度赋给numNew
ensureCapacityInternal(size + numNew); // 校验并对list2进行扩容默认容量为10
System.arraycopy(a, 0, elementData, size, numNew); // 将旧数组a的元素拷贝到elementData新数组中
size += numNew;
return numNew != 0;
}
}
public class Main {
public static void main(String[] args) {
ArrayList<String> list1 = new ArrayList<>();
list1.add("1");
list1.add("2");
list1.add("3");
ArrayList<String> list2 = new ArrayList<>();
list2.addAll(list1);
System.out.println(list2);
}
}