开发中需要用到list合并,之前没用过,记录一下,

import java.util.ArrayList;
import java.util.List;
import Bean.Entity;
public class SumList {
public static void SumList() {
List rs = new ArrayList();
List rs2 = new ArrayList();
Entity en;
for (int i = 0; i < 3; i++) {
en = new Entity();
en.setId("rs---" + i);
rs.add(en);
}
for (int i = 0; i < 4; i++) {
en = new Entity();
en.setId("rs2---" + i);
rs2.add(en);
}
rs.addAll(rs2);
for (int i = 0; i < rs.size(); i++) {
System.out.println(rs.get(i).getId());
}
}
public static void main(String[] args) {
SumList();
}
}

运行结果:

rs---0 rs---1 rs---2 rs2---0 rs2---1 rs2---2 rs2---3