public ArrayList<TabOrganizations> recursiveList(ArrayList<TabOrganizations> rootList) {
//原始数据赋值
ArrayList<TabOrganizations> FatherList = new ArrayList<TabOrganizations>();
//寻找根节点
for(TabOrganizations tabOrganizations: rootList){
if(tabOrganizations.getRoot() == 0){
FatherList.add(tabOrganizations);
}
}
//遍历父节点
for (TabOrganizations father:FatherList){
father.setChildrenList(getChildren(rootList,father.getId()));
}
return FatherList;
}
/**
*递归算法
*/
public ArrayList<TabOrganizations> getChildren(ArrayList<TabOrganizations>list,int index){
ArrayList<TabOrganizations> childrenList = new ArrayList<>();
//遍历与父节点传过来ID 匹配的子节点
for(TabOrganizations children : list){
if(children.getRoot() == index){
childrenList.add(children);
}
}
//遍历子节点的子节点
for(TabOrganizations children2 : childrenList){
children2.setChildrenList(getChildren(list,children2.getId()));
}
if(childrenList.size()==0){
return null;
}
return childrenList;
}
递归删除
/**
* 递归删除
* 所谓递归删除我的方法是把所有的子节点拿出来,再遍历删除
* 参数是传入源list集合,和需要删除的父节点
* @return
*/
List<实体类> resList = new ArrayList<实体类>();
public List<实体类> showChildren(List<实体类> oriList, Integer id){
for(实体类 t: oriList){
if(t.getParentId().equals(id)){
resList.add(t);
showChildren(oriList,t.getId);
}
}
return resList;
}
然后遍历这个数组删掉所有节点,最后删掉父节点