目的:将得到的数据通过list.set(index,string)方法存入目标数组中
示意源代码:
List<String> orgList = ["张三","李四","王五"];
List<String> desList = new ArrayList<String>(orgList.size());
for(int i=0; i<orgList.size(); i++){
desList.set(i,orgList.get(i));
}
System.out.println(desList);
结果:打印出来的结果为[]
步骤:单步跟踪调试,发现ArrayList的特点是分配的长度为这个List的长度,而size=0。查看源码发现,这里size是指List中元素的个数
解决方案:
List<String> orgList = ["张三","李四","王五"];
List<String> desList = new ArrayList<String>(orgList.size());
for(int i=0; i<orgList.size(); i++){
desList.add(i,orgList.get(i));
}
System.out.println(desList);打印结果:["张三","李四","王五"]