首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

java递归查询父类下的子类解决办法

2012-03-14 
java递归查询父类下的子类如题 表AMODULEID(PK)PROJECTNAMEMODULENOPARENTNOuuid。hex的语音01NOuuid。hex的

java递归查询父类下的子类
如题 表A
  MODULEID(PK) PROJECTNAME MODULENO PARENTNO
  uuid。hex的 语音 01 NO
  uuid。hex的 语音视频 0101 01
  uuid。hex的 语音通话 0102 01
  uuid。hex的 语音录入 0103 01


这是我想要读取的树的结构
  语音01
  --语音视频 0101
  --语音通话 0102
  --语音录入 0103
  硬件
  --声卡
  --网卡
  --主版

表就是这样的我想用递归遍历出 上边这个结构 怎么遍历啊 高手帮忙


[解决办法]

Java code
import java.util.ArrayList;import java.util.HashMap;import java.util.List;public class StudyMap {    public static void main(String[] args) {        HashMap<Integer, List<OKBean>> result = new HashMap<Integer, List<OKBean>>();        List<OKBean> testList = new ArrayList<OKBean>();        testList.add(new OKBean(1, 0));        testList.add(new OKBean(2, 0));        testList.add(new OKBean(3, 0));        testList.add(new OKBean(4, 1));        testList.add(new OKBean(5, 1));        testList.add(new OKBean(6, 1));        testList.add(new OKBean(7, 2));        testList.add(new OKBean(8, 2));        testList.add(new OKBean(9, 2));        testList.add(new OKBean(10, 4));        testList.add(new OKBean(11, 4));        testList.add(new OKBean(12, 4));        for (OKBean okbean : testList) {            List<OKBean> okbeans = result.get(okbean.getParentId());            if (okbeans == null) {                okbeans = new ArrayList<OKBean>();                result.put(okbean.getParentId(), okbeans);            }            okbeans.add(okbean);        }        displayMap(result, 1, "");    }    public static void displayMap(HashMap<Integer, List<OKBean>> result,            int startKey, String str) {        List<OKBean> list = result.get(startKey);        for (OKBean okBean : list) {            System.out.println(str + okBean.getId() + ":");            if (result.containsKey(okBean.getId())) {                displayMap(result, okBean.getId(), str + "  ");            }        }    }}class OKBean {    public OKBean() {    }    public OKBean(int id, int parentId) {        this.id = id;        this.parentId = parentId;    }    private int id;    private int parentId;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public int getParentId() {        return parentId;    }    public void setParentId(int parentId) {        this.parentId = parentId;    }} 

热点排行