import
java.util.*;
public
class
ReadConsole1 {
public
static
void
main(String[] args)
{
Tree t1=
new
Tree(
"01"
,
"name1"
,
""
);
Tree t2=
new
Tree(
"02"
,
"name2"
,
"01"
);
Tree t3=
new
Tree(
"03"
,
"name3"
,
"01"
);
Tree t4=
new
Tree(
"04"
,
"name4"
,
"02"
);
Tree t5=
new
Tree(
"05"
,
"name5"
,
"02"
);
List<Tree> list=
new
ArrayList<Tree>();
list.add(t1);
list.add(t2);
list.add(t3);
list.add(t4);
list.add(t5);
showTree(list,t1,
0
);
}
//判断字母还是数字
public
static
void
showTree(List<Tree> list,Tree tree,
int
deep)
{
String str=
""
;
for
(
int
i=
0
;i<deep;i++)
{
str+=
" "
;
}
System.out.println(str+
""
+tree.getName());
for
(
int
i=
0
;i<list.size();i++)
{
if
(list.get(i).getPid().equals(tree.getId()))
{
deep++;
showTree(list,list.get(i),deep);
deep--;
}
}
}
}
class
Tree
{
private
String id;
private
String name;
private
String pid;
public
Tree(String id,String name,String pid)
{
this
.id=id;
this
.name=name;
this
.pid=pid;
}
public
String getId() {
return
id;
}
public
String getName() {
return
name;
}
public
String getPid() {
return
pid;
}
}
来自:http://bbs.csdn.net/topics/320094100
package tree.t1;
import java.util.ArrayList;
import java.util.List;
public class ReadConsole1 {
public static void main(String[] args) {
Tree t1=new Tree(1, "name1", 0);
Tree t2=new Tree(2, "name2", 1);
Tree t3=new Tree(3, "name3", 1);
Tree t4=new Tree(4, "name4", 2);
Tree t5=new Tree(5, "name5", 2);
Tree t6=new Tree(6, "name6", 0);
Tree t7=new Tree(7, "name7", 6);
Tree t8=new Tree(8, "name8", 6);
Tree t9=new Tree(9, "name9", 7);
List<Tree> list=new ArrayList<Tree>();
list.add(t1);
list.add(t2);
list.add(t3);
list.add(t4);
list.add(t5);
list.add(t6);
list.add(t7);
list.add(t8);
list.add(t9);
List<Tree>lists=showTree(list,t1,0);
for (int i = 0; i <lists.size(); i++) {
System.out.println(lists.get(i).getId()+"----"+lists.get(i).getName());
}
}
static List<Tree> lists=new ArrayList<Tree>();
public static List<Tree> showTree(List<Tree> list,Tree tree,int deep){
String str="";
for (int i = 0; i < deep; i++) {
str+=" ";
}
lists.add(tree);
System.out.println(str+""+tree.getName());
for (int i = 0; i < list.size(); i++) {
if(list.get(i).getPid().equals(tree.getId())){
deep++;
showTree(list,list.get(i),deep);
deep--;
}
}
return lists;
}
}
class Tree{
private Integer id;
private String name;
private Integer pid;
public Tree(Integer id, String name, Integer pid) {
super();
this.id = id;
this.name = name;
this.pid = pid;
}
public Tree() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
}