zoukankan      html  css  js  c++  java
  • 最全的省份递归

    递归查询树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);

    }


    }


    }


    }

    陕西
    ----延安
    --------洛川
    ----西安

  • 相关阅读:
    xtrabackup之Innobackupex全备数据库
    没有Where条件下group by走索引
    Oracle中查看无效的对象、约束、触发器和索引
    udev/raw/asmlib/多路径 配置asm
    自适应游标共享技术02(一个简单的例子来走近ACS)
    自适应游标共享技术03(常用分析脚本)
    监控进程是否存在
    MySQL运行状态show status详解
    MySQL 加锁处理分析
    使用RMAN验证备份的有效性
  • 原文地址:https://www.cnblogs.com/zzl0916/p/11141960.html
Copyright © 2011-2022 走看看