如何编写查找父母和孩子的递归方法

编程入门 行业动态 更新时间:2024-10-28 04:20:08
本文介绍了如何编写查找父母和孩子的递归方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是为了以递归方式保存详细信息.

在这里,我想从数据库中获取详细信息,并使用递归方法将其设置到我的bean中.这样我就可以在angularUi树形对象中显示.如何编写递归方法以设置到我的bean中.

Here I want to get the details from the Database and set into my bean with a recursive method. So I can dispaly in angularUi tree formate. How can I write the recursive method to set into my beans.

我的数据库严格程度:-

我使用rowId分隔父母和孩子.您可以访问我的示例屏幕.

I am separating the parents and childs with rowId.you can access my sample screen.

例如:-父级的Rowid为1 这个1的孩子是1.1 而1.1的孩子是1.1.1,这样会延长,.

Eg:- Rowid is 1 for parent the child for this 1 is 1.1 and child for 1.1 is 1.1.1 like this it will prolong,,.

我将所有父母和孩子都保存在图片上方的一张桌子中.

I am saving all the parents and child in a single table which is above image.

每个对象(行)都有items[].如果有任何子代为父,则该子代将被添加到该items[]数组中;如果该子代有任何子代,则该子代将被添加到该行的items[]父代中.

there will be items[] for each and every object(Row). If there is any child for parent then the child will be added in that items[] array, if that child have any child then the child will be added in the parent of that row's items[]... like this it will prolong.

例如:-JSON对象为:-

{ "id": 1, "rowId": "1", "items": [ { "id": 10, "rowId": "1.1", "items": [ { "id": 100, "rowId": "1.1.1", "items": [ { "id": 1000, "rowId": "1.1.1.1", "items": [] } ] } ] }, { "id": 11, "rowId": "1.2", "items": [] } ] }

我已经使用此答案.

但是在检索时我遇到了问题.问题是检索时不会有任何父项和子项,因为数据将保存在同一表中.该关系仅是rowid.为此,我需要编写一种类似保存的递归方法,并且需要将子级添加到父级items[]数组中.

But while retriving I am facing problems. The problem is while retrieving there will not be any parent and childs as the data will be saved in same table.The relationship is only rowid. For This I need to write a recursive method like saving, and need to add the childs to parent items[] array.

public class AdminComponentBean{ List<MultiAdminComponent> componentListbean; }

MultiAdminComponent.java:-

public class MultiAdminComponent { private String componentName; private String componentIdentification; private String componentType; private String componentState; private String componentUrl; private String rowId; private List<MultiAdminComponent> items; }

在这里,我尝试检索所有详细信息,并尝试将子级添加到父级.但这应该是一种递归方法

Here I try to retrive all the details and trying to add the childs to parent.But it should make a recursive method

List<MultiAdminComponent> componentList=BaseDAO.getAdminComponentDAOObject().getComponentDetails(); if(null != componentList) { for(MultiAdminComponent itemsList : componentList){ if(itemsList.getRowId().length().equals() "1"){//here parent row will come //by considering rowid I need to find the child of the rowId //child of 1 is 1.1 //if 1.1 is child of 1 then I need to add that 1.1 object to `items[]` array of 1 //like this it should work recursve } } }

推荐答案

我建议不要采取递归的方式,而是将所有元素都存储在HashMap中

Instead of recursive I would suggest to go an extra step and store all elements in a HashMap

// a map containing all elements searchable by the rowId HashMap<String, MultiAdminComponent> idToItem = new HashMap<>(); // a set containing all elements that don't have a parent (id: 1, 2, 3, etc.) Set<MultiAdminComponent> rootItems = new HashSet<>(); for (MultiAdminComponent item : componentList) { // build the id->item map idToItem.put(item.getRowId(), item); } for (MultiAdminComponent item : componentList) { String parentId = getParentId(item.getRowId()); if (parentId == null) { // this item has no parent -> it is a root item rootItems.add(item); } else { // This item has a parent -> look the parent up MultiAdminComponent parent = idToItem.get(parentId); parent.getItems().add(item); } } // rootItems now contains all MultiAdminComponents which do not have a parent, with the correct hierarchy for all items

getParentId可能是这样的:

getParentId could be something like this:

private String getParentId(String id) { int lastDot = id.lastIndexOf("."); if (lastDot == -1) { return null; } return id.substring(0, lastDot); }

如果可以保证componentList将从父级到子级遍历整个列表,则可以将两个for循环组合在一起.

If you can guarantee that componentList will go through the list from parents to children, you can combine both for-loops.

更多推荐

如何编写查找父母和孩子的递归方法

本文发布于:2023-10-26 09:16:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529733.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   父母   孩子   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!