Java递归如何正确输出树形菜单
1个回答
展开全部
首先我们要建立树节点的类:
[java] view plain copy
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;
}
}
输出树形菜单类:
[java] view plain copy
package com.tree;
import java.util.ArrayList;
import java.util.List;
public class Tree {
private StringBuffer html = new StringBuffer();
private List<Node> nodes;
public Tree(List<Node> nodes){
this.nodes = nodes;
}
public String buildTree(){
html.append("<ul>");
for (Node node : nodes) {
Integer id = node.getId();
if (node.getParentId() == ) {
html.append("\r\n<li id='" + id + "'>" + node.getName()+ "</li>");
build(node);
}
}
html.append("\r\n</ul>");
return html.toString();
}
private void build(Node node){
List<Node> children = getChildren(node);
if (!children.isEmpty()) {
html.append("\r\n<ul>");
for (Node child : children) {
Integer id = child.getId();
html.append("\r\n<li id='" + id + "'>" + child.getName()+ "</li>");
build(child);
}
html.append("\r\n</ul>");
}
}
private List<Node> getChildren(Node node){
List<Node> children = new ArrayList<Node>();
Integer id = node.getId();
for (Node child : nodes) {
if (id.equals(child.getParentId())) {
children.add(child);
}
}
return children;
}
}
[java] view plain copy
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;
}
}
输出树形菜单类:
[java] view plain copy
package com.tree;
import java.util.ArrayList;
import java.util.List;
public class Tree {
private StringBuffer html = new StringBuffer();
private List<Node> nodes;
public Tree(List<Node> nodes){
this.nodes = nodes;
}
public String buildTree(){
html.append("<ul>");
for (Node node : nodes) {
Integer id = node.getId();
if (node.getParentId() == ) {
html.append("\r\n<li id='" + id + "'>" + node.getName()+ "</li>");
build(node);
}
}
html.append("\r\n</ul>");
return html.toString();
}
private void build(Node node){
List<Node> children = getChildren(node);
if (!children.isEmpty()) {
html.append("\r\n<ul>");
for (Node child : children) {
Integer id = child.getId();
html.append("\r\n<li id='" + id + "'>" + child.getName()+ "</li>");
build(child);
}
html.append("\r\n</ul>");
}
}
private List<Node> getChildren(Node node){
List<Node> children = new ArrayList<Node>();
Integer id = node.getId();
for (Node child : nodes) {
if (id.equals(child.getParentId())) {
children.add(child);
}
}
return children;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询