试试这个吧public class T { public static void main(String [] args){ T t = new T(); t.init(); } public void init(){ List<Tree> tchild = new ArrayList<Tree>(); List<Tree> t3child = new ArrayList<Tree>(); Tree t = new Tree(); t.id = 1; t.name = "level--1"; Tree t2 = new Tree(); t2.id = 2; t2.name = "level--2"; t2.parant = t; tchild.add(t2); Tree t3 = new Tree(); t3.id = 3; t3.name = "level--3"; t3.parant = t; tchild.add(t3); t.childs = tchild; Tree t4 = new Tree(); t4.id = 4; t4.name = "level--4"; t4.parant = t3; t3child.add(t4); t3.childs = t3child; show(t,0);
T t = new T();
t.init();
}
public void init(){
List<Tree> tchild = new ArrayList<Tree>();
List<Tree> t3child = new ArrayList<Tree>();
Tree t = new Tree();
t.id = 1;
t.name = "level--1";
Tree t2 = new Tree();
t2.id = 2;
t2.name = "level--2";
t2.parant = t;
tchild.add(t2);
Tree t3 = new Tree();
t3.id = 3;
t3.name = "level--3";
t3.parant = t;
tchild.add(t3);
t.childs = tchild;
Tree t4 = new Tree();
t4.id = 4;
t4.name = "level--4";
t4.parant = t3;
t3child.add(t4);
t3.childs = t3child;
show(t,0);
}
public void show(Tree t,int deep){
String str="";
for(int i=0;i<deep;i++)
{
str+="-";
}
System.out.println(str+""+t.name);
List<Tree> childs = t.childs;
if(childs!=null && !childs.isEmpty()){
for(Tree child :childs){
deep++;
show(child,deep);
deep--;
}
}
}
class Tree{
private int id;
private String name;
private Tree parant;
private List<Tree> childs;
}
}