Java递归如何正确输出树形菜单
程序员文章站
2024-03-08 11:20:42
本文实例为大家分享了java递归输出树形菜单的具体代码,供大家参考,具体内容如下
首先我们要建立树节点的类:
package com.tree;...
本文实例为大家分享了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<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; } }
然后我们来测试一下:
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()); } }
输出的结果:
<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>
浏览器效果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。