[英]How to add a list of objects to another class that has a member that is a list of objects?
我有一個 class, Omega
,它的成員是List<Alpha>
。 如果我有一個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;
}
}
}
首先,在 Omega 中初始化列表:
public class Omega {
public int number;
public List<Alpha> beta = new ArrayList<>();
}
然后將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.