Java遞歸如何正確輸出樹形菜單
本文實(shí)例為大家分享了java遞歸輸出樹形菜單的具體代碼,供大家參考,具體內(nèi)容如下
首先我們要建立樹節(jié)點(diǎn)的類:
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<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() == null) {
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;
}
}
然后我們來(lái)測(cè)試一下:
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<Node> nodes = new ArrayList<Node>();
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");
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());
}
}
輸出的結(jié)果:
<ul> <li id='1'>node1</li> <ul> <li id='11'>node11</li> <ul> <li id='111'>node111</li> </ul> <li id='12'>node12</li> </ul> <li id='2'>node2</li> <ul> <li id='21'>node21</li> </ul> <li id='3'>node3</li> </ul>
瀏覽器效果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)戰(zhàn)員工績(jī)效管理系統(tǒng)的實(shí)現(xiàn)流程
只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+Mysql+Maven+HTML實(shí)現(xiàn)一個(gè)員工績(jī)效管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2022-01-01
SpringBoot中jar啟動(dòng)下如何讀取文件路徑
這篇文章主要介紹了SpringBoot?jar啟動(dòng)下如何讀取文件路徑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Java實(shí)現(xiàn)不同的類的屬性之間相互賦值
今天小編就為大家分享一篇關(guān)于Java實(shí)現(xiàn)不同的類的屬性之間相互賦值,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03
Java與SpringBoot對(duì)redis的使用方式
這篇文章主要介紹了Java與SpringBoot對(duì)redis的使用方式,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08
idea如何在service窗口中顯示多個(gè)服務(wù)
這篇文章主要介紹了idea如何在service窗口中顯示多個(gè)服務(wù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Spring Boot 整合 MyBatis 連接數(shù)據(jù)庫(kù)及常見問題
MyBatis 是一個(gè)優(yōu)秀的持久層框架,支持定制化 SQL、存儲(chǔ)過程以及高級(jí)映射,下面詳細(xì)介紹如何在 Spring Boot 項(xiàng)目中整合 MyBatis 并連接數(shù)據(jù)庫(kù),感興趣的朋友一起看看吧2025-03-03

