有两种方式:
1.循环
循环第一个list,然后每循环一次,就给新的list赋值
List<String> oneList = new ArrayList<String>();
List<String> twoList = new ArrayList<String>();
oneList.add("1");
oneList.add("2");
oneList.add("3");
for(String str : oneList){
twoList.add(str);
}
for(String str : twoList){
System.out.println(str);
}
结果:
1
2
3
2.arrayList的addAll()方法
List<String> oneList = new ArrayList<String>();
oneList.add("1");
oneList.add("2");
oneList.add("3");
for(String str : oneList){
System.out.println("one:"+str);
}
List<String> twoList = new ArrayList<String>();
twoList.add("4");
twoList.addAll(oneList);
for(String str : twoList){
System.out.println("two:"+str);
}
结果:
one:1
one:2
one:3
two:4
two:1
two:2
two:3