递归(心得体会)

您所在的位置:网站首页 递归算法实验体会 递归(心得体会)

递归(心得体会)

2024-07-10 15:35| 来源: 网络整理| 查看: 265

在这里插入图片描述 把这种的递归,参数是PID

@RequestMapping(value="/queryListMenu/{pid}",method=RequestMethod.GET) public List queryMenu(@PathVariable(value = "pid",required = true) String pid) { List list = new ArrayList(); List listMenuParent = new ArrayList(); Boolean flag = true; while (flag) { //menuParent中,没有Listchild 为 null Menu menuParent = orgService.findByPid(pid); //如果最后一条数据为null则把之前的数据赋给list并返回作为最后输出 list = listMenuParent; //如果最后一条数据为null则flag为false,跳出if判断 flag = false; //这里pid 是作为child的id的 Menu menuChild = orgService.findById(pid); if (menuChild != null && menuParent != null) { List listChild = new ArrayList(1); /*String s = JSONObject.toJSONString(menuChild); Object parse = JSON.parse(s);*/ listChild.add(menuChild); //一个完整的实体类对象,有Listchild menuParent.setChild(listChild); //一个一个menu放入集合 //listMenuParent.add(menuParent); listMenuParent.add(menuParent); if (StringUtils.isNotBlank(menuChild.getPid())){ pid = menuChild.getPid(); //当为true时又返回if判断 flag = true; }else { //如果最后一条PID为空则直接返回之前的数据 return listMenuParent; } } } return list; } @Entity @Table(name = "sys_menu") public class Menu implements Serializable { @Id @Column(name = "ID") private String id; @Column(name = "NAME") private String name; @Column(name = "PID") private String pid; @Transient @JsonInclude(JsonInclude.Include.NON_NULL) private List child;

输出:

[ { "id": "1", "name": "LJ", "pid": "2", "child": [ { "id": "2", "name": "ZF", "pid": "3", "child": [ { "id": "3", "name": "SP", "pid": "4", "child": [ { "id": "4", "name": "WS", "pid": "5", "child": [ { "id": "5", "name": "HXM", "pid": "6", "child": [ { "id": "6", "name": "GMY", "pid": "7", "child": null } ] } ] } ] } ] } ] }, { "id": "2", "name": "ZF", "pid": "3", "child": [ { "id": "3", "name": "SP", "pid": "4", "child": [ { "id": "4", "name": "WS", "pid": "5", "child": [ { "id": "5", "name": "HXM", "pid": "6", "child": [ { "id": "6", "name": "GMY", "pid": "7", "child": null } ] } ] } ] } ] }, { "id": "3", "name": "SP", "pid": "4", "child": [ { "id": "4", "name": "WS", "pid": "5", "child": [ { "id": "5", "name": "HXM", "pid": "6", "child": [ { "id": "6", "name": "GMY", "pid": "7", "child": null } ] } ] } ] }, { "id": "4", "name": "WS", "pid": "5", "child": [ { "id": "5", "name": "HXM", "pid": "6", "child": [ { "id": "6", "name": "GMY", "pid": "7", "child": null } ] } ] }, { "id": "5", "name": "HXM", "pid": "6", "child": [ { "id": "6", "name": "GMY", "pid": "7", "child": null } ] } ]

在这里插入图片描述 总结:不同位置(i=0,i=1等)不同引用,但是相同对象的。如果对这个对象set,那么在不同位置都可以看到set的值

原本想得到的数据是如下这种但是发现根本不行:

[ { "id": "1", "name": "LJ", "pid": "2", "child": [ { "id": "2", "name": "ZF", "pid": "3", "child": null }] }, { "id": "2", "name": "LJ", "pid": "3", "child": [ { "id": "3", "name": "ZF", "pid": "4", "child": null }] }, { "id": "4", "name": "LJ", "pid": "5", "child": [ { "id": "5", "name": "ZF", "pid": "6", "child": null }] } ]

想实现上述这种格式只能:

泛型改为Object

private List child; @Entity @Table(name = "sys_menu") public class Menu implements Serializable { @Id @Column(name = "ID") private String id; @Column(name = "NAME") private String name; @Column(name = "PID") private String pid; @Transient @JsonInclude(JsonInclude.Include.NON_NULL) private List child; @RequestMapping(value="/queryListMenu/{pid}",method=RequestMethod.GET) public List queryMenu(@PathVariable(value = "pid",required = true) String pid) { List list = new ArrayList(); List listMenuParent = new ArrayList(); Boolean flag = true; while (flag) { //menuParent中,没有Listchild 为 null Menu menuParent = orgService.findByPid(pid); //如果最后一条数据为null则把之前的数据赋给list并返回作为最后输出 list = listMenuParent; //如果最后一条数据为null则flag为false,跳出if判断 flag = false; //这里pid 是作为child的id的 Menu menuChild = orgService.findById(pid); if (menuChild != null && menuParent != null) { List listChild = new ArrayList(); //object对象中去掉为null的child String jsonMenu = JSONObject.toJSONString(menuChild); Object parse = JSON.parse(jsonMenu); listChild.add(parse); //一个完整的实体类对象,有Listchild menuParent.setChild(listChild); //一个一个menu放入集合 listMenuParent.add(menuParent); if (StringUtils.isNotBlank(menuChild.getPid())){ pid = menuChild.getPid(); //当为true时又返回if判断 flag = true; }else { //如果最后一条PID为空则直接返回之前的数据 return listMenuParent; } } } return list; }

注意这段代码(看注释理解):

//object对象中去掉为null的child,目的是下一次在走到这个位置set的值不在上一次看到 String jsonMenu = JSONObject.toJSONString(menuChild); Object parse = JSON.parse(jsonMenu);


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3