Java递归遍历树形结构
程序员文章站
2024-03-09 16:30:18
废话不多说了,直接给大家贴代码,具体代码如下所示:
//菜单树形结构
public jsonarray treemenulist(jsonarray menu...
废话不多说了,直接给大家贴代码,具体代码如下所示:
//菜单树形结构 public jsonarray treemenulist(jsonarray menulist, int parentid) { jsonarray childmenu = new jsonarray(); for (object object : menulist) { jsonobject jsonmenu = jsonobject.fromobject(object); int menuid = jsonmenu.getint("id"); int pid = jsonmenu.getint("parentid"); if (parentid == pid) { jsonarray c_node = treemenulist(menulist, menuid); jsonmenu.put("childnode", c_node); childmenu.add(jsonmenu); } } return childmenu; } public static void main(string args[]) { menucacheservice menucacheservice = new menucacheservice(); jsonarray jsonarray = new jsonarray(); menu menu1 = new menu(); menu1.setid(1l); menu1.setparentid(0); menu1.setlevel(0); menu menu2 = new menu(); menu2.setid(2l); menu2.setparentid(0); menu2.setlevel(0); menu menu3 = new menu(); menu3.setid(3l); menu3.setparentid(2); menu3.setlevel(1); menu menu4 = new menu(); menu4.setid(4l); menu4.setparentid(2); menu4.setlevel(1); menu menu5 = new menu(); menu5.setid(5l); menu5.setparentid(4); menu5.setlevel(2); menu menu6 = new menu(); menu6.setid(6l); menu6.setparentid(1); menu6.setlevel(1); jsonarray.add(menu1); jsonarray.add(menu2); jsonarray.add(menu3); jsonarray.add(menu4); jsonarray.add(menu5); jsonarray.add(menu6); system.out.print(menucacheservice.treemenulist(jsonarray,0)); }
ps:java实现树的递归遍历(用于实现折叠菜单)
1.核心算法
package com.zf.tag.dao; import java.util.arraylist; import java.util.list; import com.zf.tag.entity.dept.dept; public class deptdao extends basedao{ public static list<dept> searchalldept() throws exception{ list<object[]> listallobject=new arraylist<object[]>(); list<object[]> tmp=new arraylist<object[]>(); list<dept> listalldept=new arraylist<dept>(); string sql="select id,dname,up_did from tbl_dept"; listallobject=getresult(sql); for (object[] row : listallobject) { object[] ro = new object[3]; ro[0] = row[0]; ro[1] = row[1]; ro[2] = row[2]; tmp.add(row); } for (object[] row : tmp) { if (null == row[2]) { dept d = new dept(); d.setid(integer.parseint(string.valueof(row[0]))); d.setdname(string.valueof(row[1])); listalldept.add(d); fomatedept(tmp, d); } } return listalldept; } private static void fomatedept(list<object[]> tmp, dept dept) { for (object[] row : tmp) { if ((null != row[2]) && integer.parseint(string.valueof(row[2])) == dept.getid()) { dept d = new dept(); d.setid(integer.parseint(string.valueof(row[0]))); d.setdname(string.valueof(row[1])); list<dept> list = dept.getchildren(); if (list == null) { list = new arraylist<dept>(); } list.add(d); dept.setchildren(list); fomatedept(tmp, d); } } } }
2.实体类(部门)
package com.zf.tag.entity.dept; import java.util.list; public class dept { private integer id; private string dname; private list<dept> children; public integer getid() { return id; } public void setid(integer id) { this.id = id; } public string getdname() { return dname; } public void setdname(string dname) { this.dname = dname; } public list<dept> getchildren() { return children; } public void setchildren(list<dept> children) { this.children = children; } public string tostring(){ return this.getdname(); } }
以上所述是小编给大家介绍的java递归遍历树形结构的相关内容,希望对大家有所帮助!
下一篇: asp.net 结合YUI 3.0小示例