java创建树形结构数据.docx
- 文档编号:10402801
- 上传时间:2023-02-11
- 格式:DOCX
- 页数:8
- 大小:15.37KB
java创建树形结构数据.docx
《java创建树形结构数据.docx》由会员分享,可在线阅读,更多相关《java创建树形结构数据.docx(8页珍藏版)》请在冰豆网上搜索。
java创建树形结构数据
1.创建节点对象
packagetest;
importjava.util.ArrayList;
importjava.util.List;
/**
*节点
*
*@classNode
*/
publicclassNode{
privateStringid;
privateStringname;
/**
*节点等级,等级越高数值越小默认"0"最高
*/
privateStringlevel;
privateStringparentId;
privateList
publicNode(){
this.children=newArrayList
}
publicvoidaddChildren(Nodenode){
this.children.add(node);
}
@Override
publicStringtoString(){
return"{id:
"+this.id+",name:
"+this.name+
",parentId:
"+this.parentId+
",level:
"+this.level+
",children:
"+this.children+"}";
}
publicStringgetId(){
returnid;
}
publicvoidsetId(Stringid){
this.id=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetLevel(){
returnlevel;
}
publicvoidsetLevel(Stringlevel){
this.level=level;
}
publicStringgetParentId(){
returnparentId;
}
publicvoidsetParentId(StringparentId){
this.parentId=parentId;
}
publicList
returnchildren;
}
publicvoidsetChildren(List
this.children=children;
}
}
2.创建主体类
packagetest;
importjava.util.ArrayList;
importjava.util.List;
publicclassTree{
publicstaticList
List
Noden01=newNode();
n01.setId("1");
n01.setName("node01");
n01.setLevel("0");
n01.setParentId("0");
Noden02=newNode();
n02.setId("2");
n02.setName("node02");
n02.setLevel("0");
n02.setParentId("0");
Noden03=newNode();
n03.setId("3");
n03.setName("node03");
n03.setLevel("0");
n03.setParentId("0");
Noden04=newNode();
n04.setId("4");
n04.setName("node04");
n04.setLevel("1");
n04.setParentId("1");
Noden05=newNode();
n05.setId("5");
n05.setName("node05");
n05.setLevel("2");
n05.setParentId("4");
//数据集合要注意排序,等级越低越靠前
list.add(n05);
list.add(n04);
list.add(n03);
list.add(n02);
list.add(n01);
returnlist;
}
publicstaticvoidcreateTree(List
StringlowLevel=list.get(0).getLevel();
List
//当等级最高时,停止排序
if(!
"0".equals(lowLevel)){
//将最低级别的放到数组中
for(Nodenode:
list){
if(lowLevel.equals(node.getLevel()))
children.add(node);
else
break;
}
//增加遍历效率
list.removeAll(children);
for(Nodechild:
children){
for(Nodeparent:
list){
if(parent.getId().equals(child.getParentId()))
parent.addChildren(child);
}
}
createTree(list);
}
}
publicstaticvoidmain(String[]args){
List
//创建前
System.out.println(list);
Tree.createTree(list);
//创建后
System.out.println(list);
}
}
3.结果
//创建前
[{
id:
5,
name:
node05,
parentId:
4,
level:
2,
children:
[]
},
{
id:
4,
name:
node04,
parentId:
1,
level:
1,
children:
[]
},
{
id:
3,
name:
node03,
parentId:
0,
level:
0,
children:
[]
},
{
id:
2,
name:
node02,
parentId:
0,
level:
0,
children:
[]
},
{
id:
1,
name:
node01,
parentId:
0,
level:
0,
children:
[]
}]
//创建后
[{
id:
3,
name:
node03,
parentId:
0,
level:
0,
children:
[]
},
{
id:
2,
name:
node02,
parentId:
0,
level:
0,
children:
[]
},
{
id:
1,
name:
node01,
parentId:
0,
level:
0,
children:
[{
id:
4,
name:
node04,
parentId:
1,
level:
1,
children:
[{
id:
5,
name:
node05,
parentId:
4,
level:
2,
children:
[]
}]
}]
}]
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 创建 树形 结构 数据
![提示](https://static.bdocx.com/images/bang_tan.gif)