Java递归如何正确输出树形菜单

网友投稿 274 2023-06-15


Java递归如何正确输出树形菜单

本文实例为大家分享了java递归输出树形菜单的具体代码,供大家参考,具体内容如下

首先我们要建立树节点的类:

package com.tree;

public class Node {

private Integer id;

private Integer parentId;

private String name;

private String link;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public Integer getParentId() {

return parentId;

}

public void setParentId(Integer parentId) {

this.parentId = parentId;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getLink() {

return link;

}

public void setLink(String link) {

this.link = link;

}

}

输出树形菜单类:

package com.tree;

import java.util.ArrayList;

import java.util.List;

public class Tree {

private StringBuffer html = new StringBuffer();

private List nodes;

public Tree(List nodes){

this.nodes = nodes;

}

public String buildTree(){

html.append("

for (Node node : nodes) {

Integer id = node.getId();

if (node.getParentId() == null) {

html.append("\r\n

build(node);

}

}

html.append("\r\n

return html.toString();

}

private void build(Node node){

List children = getChildren(node);

if (!children.isEmpty()) {

html.append("\r\n

for (Node child : children) {

Integer id = child.getId();

html.append("\r\n

build(child);

}

html.append("\r\n

}

}

private List getChildren(Node node){

List children = new ArrayList();

Integer id = node.getId();

for (Node child : nodes) {

if (id.equals(child.getParentId())) {

children.add(child);

}

}

return children;

}

}

然后我们来测试一下:

import java.util.ArrayList;

import java.util.List;

import cn.com.tree.Node;

import cn.com.tree.Tree;

public class Test {

/**

* @param args

*/

public static void main(String[] args) {

List nodes = new ArrayList();

Node node1 = new Node();

node1.setId(1);

node1.setName("node1");

node1.setParentId(null);

node1.setLink(null);

nodes.add(node1);

Node node11 = new Node();

node11.setId(11);

node11.setName("node11");

node11.setParentId(1);

node11.setLink(null);

nodes.add(node11);

Node node111 = new Node();

node111.setId(111);

node111.setName("node111");

node111.setParentId(11);

node111.setLink(null);

nodes.add(node111);

Node node12 = new Node();

node12.setId(12);

node12.setName("node12");

node12.setParentId(1);

node12.setLink(null);

nodes.add(node12);

Node node2 = new Node();

node2.setId(2);

node2.setName("node2");

node2.setParentId(null);

node2.setLink(null);

nodes.add(node2);

Node node21 = new Node();

node21.setId(21);

node21.setName("node21");

http://node21.setParentId(2);

node21.setLink(null);

nodes.add(node21);

Node node3 = new Node();

node3.setId(3);

node3.setName("node3");

node3.setParentId(null);

node3.setLink(null);

nodes.add(node3);

Tree tree = new Tree(nodes);

System.out.println(tree.buildTree());

}

}

输出的结果:

浏览器效果:


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java仿文库的基本方法(openoffice+swftools+flexPaper)
下一篇:URL中“#” “?” &“”号的作用浅析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~