递归查询树tree结构有两种做法:
第一种,递归查询数据库结构,
第二种,一次性将数据库表中的所有数据查出来,然后再递归查出来的list集合,
第一种做法适合数据量较少的tree结构,因为要一直查询数据库数据量大时速度回相对较慢,所以数据量大时建议使用第二种方法
public class G { public static void main(String[] args) { List provinceList = new ArrayList(); provinceList.add(new Province(0, 1, "陕西")); provinceList.add(new Province(1, 2, "延安")); provinceList.add(new Province(2, 3, "洛川")); provinceList.add(new Province(1, 4, "西安")); findAllInProvince(provinceList, 0, ""); } public static class Province { private int parent = 0; private int children = 0; private String info = ""; public int getParent() { return parent; } public int getChildren() { return children; } public String getInfo() { return info; } public Province(int parent, int children, String info) { this.parent = parent; this.children = children; this.info = info; } } public static void findAllInProvince(List provinceList, int parent, String str) { if (parent != 0) { str += "----"; } for (int j = 0; provinceList != null && j < provinceList.size(); j++) { Province province = (Province) provinceList.get(j); Integer children = null; if (province != null && province.getParent() == parent) { children = province.getChildren(); System.out.println(str + province.getInfo()); findAllInProvince(provinceList, children, str); } } }}陕西----延安--------洛川----西安
转载于:https://www.cnblogs.com/zzl0916/p/11141960.html
相关资源:javascript入门笔记