public class DepartmentResp { private String bumenbm; private String fbumenbm; private String bumenmch; private List<DepartmentResp> children = new ArrayList<DepartmentResp>(); } 如何使用递归取到 children中的最底层的DepartmentResp并设置每个bumenmch
时间: 2024-03-08 17:50:19 浏览: 54
children-json.zip
可以通过递归遍历children列表来找到最底层的DepartmentResp对象,并设置每个bumenmch属性。
示例代码如下:
```
public void setBumenmchForChildren(DepartmentResp departmentResp, String bumenmch) {
if (departmentResp.getChildren() != null && !departmentResp.getChildren().isEmpty()) {
for (DepartmentResp child : departmentResp.getChildren()) {
setBumenmchForChildren(child, bumenmch);
}
} else {
departmentResp.setBumenmch(bumenmch);
}
}
```
使用方法如下:
```
DepartmentResp rootDepartment = ...; // 根节点
setBumenmchForChildren(rootDepartment, "最底层的部门名称");
```
这样就可以递归遍历children列表,找到最底层的DepartmentResp对象,并设置每个bumenmch属性。
阅读全文