package com.mr.solon.test;
import com.mr.solon.test.dd.Bacod;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
List<Bacod> bacodList = new ArrayList<>();
// 添加对象到数组中
bacodList.add(new Bacod(1L, 0L));
bacodList.add(new Bacod(2L, 0L));
bacodList.add(new Bacod(3L, 1L));
bacodList.add(new Bacod(4L, 1L));
bacodList.add(new Bacod(5L, 4L));
bacodList.add(new Bacod(6L, 2L));
//假设已直顶节点
List<Bacod> up = new ArrayList<>();
up.add(new Bacod(1L, 0L));
up.add(new Bacod(2L, 0L));
List<Bacod> result = new ArrayList<>();
for (Bacod top : up) {
result.add(top);
findChildren(bacodList, top, result);
}
// 输出结果
for (Bacod bacod : result) {
System.out.println(bacod);
}
}
private static void findChildren(List<Bacod> bacodList, Bacod parent, List<Bacod> result) {
for (Bacod bacod : bacodList) {
if (bacod.getPid().equals(parent.getId())) {
result.add(bacod);
findChildren(bacodList, bacod, result);
}
}
}
}
java通过递归把树形数据数据组合成数(方便存入excel)
最新推荐文章于 2024-11-01 14:48:20 发布