[英]How to add a list of objects to another class that has a member that is a list of objects?
I've got a class, Omega
, that has as a member a List<Alpha>
.我有一个 class, Omega
,它的成员是List<Alpha>
。 If I then had a list of Omega
s, how would I add a list of Alpha
s to get Omega
?如果我有一个Omega
的列表,我将如何添加一个Alpha
的列表来获得Omega
?
public class testClass{
public static void main(String[] args) {
List <Omega> omegaA = new LinkedList<Omega>();
List <Alpha> alphaA = new LinkedList<Alpha>();
testMethod(alphaA);
}
public void testMethod(List<Omega> blue){
for (int i = 0; i < blue.size(); i++){
Alpha x = new Alpha();
Alpha z = new Alpha();
x.setBob(i);
z.setBob(i);
}
//How to add x and z as a list to the first blue in the list?
//If blue had 2 entries in the list, how to add them to the second item in list?
}
public class Omega{
public int number;
public List<Alpha> beta;
}
public class Alpha{
public int bob;
public int getBob() {
return bob;
}
public void setBob(int x) {
this.bob = bob;
}
}
}
First, initialize the list in Omega:首先,在 Omega 中初始化列表:
public class Omega {
public int number;
public List<Alpha> beta = new ArrayList<>();
}
then to add x
and z
to each Omega
:然后将x
和z
添加到每个Omega
:
for (int i = 0; i < blue.size(); i++){
Alpha x = new Alpha();
Alpha z = new Alpha();
x.setBob(i);
z.setBob(i);
blue.get(i).add(x);
blue.get(i).add(z);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.