Flask接口签名sign原理与实例代码浅析
229
2023-07-20
Java递归遍历树形结构
废话不多说RaROgEU了,直接给大家贴代码,具体代码如下所示:
//菜单树形结构
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
List
List
List
String sql="selectRaROgEU 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
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
if (list == null) {
list = new ArrayList
}
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
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
return children;
}
public void setChildren(List
this.children = children;
}
public String toString(){
return this.getDname();
}
}
以上所述是给大家介绍的Java递归遍历树形结构的相关内容,希望对大家有所帮助!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~