list复制时,需要首先保证目标list空间足够大,并非指size大小,而是实际容纳大小。
package com.bluejie.bluej.java.base.container;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class ListCopyDemo {
private static ListCopyDemolistCopyDemo =new ListCopyDemo();
public static void main(String[] args) {
listCopyDemo.copy();
}
private void copy(){
List from =new ArrayList();
from.add("1");
from.add("2");
from.add("3");
//size大小于实际存储数量不同
List to =new ArrayList(Arrays.asList(new Object[from.size()]));
Collections.copy(to,from);
System.out.println(to.size());
}
}