实体类 A
public class A {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
用=会报错
public static void main(String[] args) {
List<A>[] lists=new ArrayList[4];
List<A> list= new ArrayList<>();
A a=new A();
a.setName("小米");
a.setAge(8);
list.add(a);
A a1=new A();
a1.setName("小明");
a1.setAge(8);
list.add(a1);
A a2=new A();
a2.setName("小红");
a2.setAge(8);
list.add(a2);
A a3=new A();
a3.setName("小绿");
a3.setAge(8);
list.add(a3);
for (int i = 0; i < list.size(); i++) {
List<A> list1=list.subList(i,i+1);
lists[i]=list1;
}
}
用 addAll就不报错
public static void main(String[] args) {
List<A>[] lists=new ArrayList[4];
List<A> list= new ArrayList<>();
A a=new A();
a.setName("小米");
a.setAge(8);
list.add(a);
A a1=new A();
a1.setName("小明");
a1.setAge(8);
list.add(a1);
A a2=new A();
a2.setName("小红");
a2.setAge(8);
list.add(a2);
A a3=new A();
a3.setName("小绿");
a3.setAge(8);
list.add(a3);
for (int i = 0; i < list.size(); i++) {
List<A> alist=new ArrayList<>();
List<A> list1=list.subList(i,i+1);
alist.addAll(list1);
lists[i]=alist;
}
}