日期:2014-05-20 浏览次数:20899 次
private static HashMap<String,ArrayList<String>> tmpMap=new HashMap<String,ArrayList<String>>(); private static void putData(String pid,String id) { ArrayList<String> childList=tmpMap.get(pid); if(childList==null) { childList=new ArrayList<String>(); tmpMap.put(pid, childList); } childList.add(id); } private static void makeTree(String pid,int level) { System.out.println("level:"+level+","+pid); ArrayList<String> childList=tmpMap.get(pid); if(childList!=null) { for(int i=0;i<childList.size();i++) { makeTree(childList.get(i),level+1); } } } public static void main(String[] args) { putData("A","B"); putData("A","C"); putData("B","D"); putData("B","G"); System.out.println(tmpMap.toString()); makeTree("A",1); }