在这里,我想得到父母和孩子的孩子。 这是我的Screen.
在此屏幕中,我想保存所有行。为此我创造了下面的豆子。
public class AdminComponentBean{
List<MultiAdminComponent> componentListbean;
}
另一个Bean: -
public class MultiAdminComponent {
private String componentName;
private String componentIdentification;
private String componentType;
private String componentState;
private String componentUrl;
private String rowId;
private List<MultiAdminComponent> items;
}
在我的服务中,我尝试检索所有孩子。但我无法得到父母的孩子。
List < MultiAdminComponent > adminComponentList = adminComponentBean.getComponentListbean();
for (MultiAdminComponent adminComponentListBean: adminComponentList) {
flag = BaseDAO.getAdminComponentDAOObject().saveParentComponentDetails(adminComponentListBean);//Here the parents will save but not the childs
for (MultiAdminComponent adminComponentchild: adminComponentListBean.getItems()) {//here I am trying to save childs
}
}
答案 0 :(得分:3)
您可以编写这样的递归方法:
void addChildren(MultiAdminComponent parent, List<MultiAdminComponent> children) {
if(null != parent.getItems()) {
for(MultiAdminComponent child : parent.getItems()) {
children.add(child);
addChildren(child, children);
}
}
}
并用空列表调用它,例如:
MultiAdminComponent parent; //parent
List<MultiAdminComponent> children = new ArrayList<>();
addChildren(parent, children);
在方法调用之后,children
列表应该包含所有子对象。