我正在尝试从parent level
到each child level
遍历菜单列表。我的目标是获取每个child menu
的名称与上层层次结构名称的连接。 即父项->子项->子子项->子子项
我编写了以下递归代码段以遍历列表,但无法获得所需的结果。谁能指导我做错了什么,或者如果有更好的方法可以做到这一点,请也指导。
源代码:
private void prepareClassifier(List<Menu> menuList)
{
String classifierName = "";
for(Menu mi : menuList)
{
classifierName = mi.getNameEng();
if(mi.getChildMenu().size() > 0)
getClassifier(mi.getChildMenu(),mi.getChildMenu().size(),classifierName);
}
}
private void getClassifier(List<Menu> childMenu, int childListSize,String name) {
for(int index=0;index < childListSize;index++)
{
if(!childMenu.get(index).getChildMenu().isEmpty())
{
name = name.concat("_").concat(childMenu.get(index).getNameEng());
getClassifier(childMenu.get(index).getChildMenu(), childMenu.get(index).getChildMenu().size(), name);
}
else
{
name = name.concat("_").concat(childMenu.get(index).getNameEng());
classifierList.add(name);
name = name.substring(0,name.length()-childMenu.get(index).getNameEng().length()-1);
}
}
}
Menu.java
public class Menu {
@JsonProperty("menuID")
private String menuID;
@JsonProperty("nameKin")
private String nameKin;
@JsonProperty("nameEng")
private String nameEng;
@JsonProperty("menuStatus")
private String menuStatus;
@JsonProperty("menuParentID")
private String menuParentID;
@JsonProperty("lvl")
private Integer lvl;
@JsonProperty("prioritylist")
private String prioritylist;
@JsonProperty("quickMenu")
private String quickMenu;
@JsonProperty("menuType")
private String menuType;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonIgnore
private List<Menu> menu = new ArrayList<Menu>();
public Menu getchildMenu (int i)
{
while (menu.size () <= i)
addChildMenu (new Menu ());
return menu.get (i);
}
public void setchildMenu (Menu childMenu, int i)
{
this.menu.set (i, childMenu);
}
public ArrayList<Menu> getChildMenu ()
{
return (ArrayList<Menu>) menu;
}
public void setChildMenu (ArrayList<Menu> childMenu)
{
this.menu = childMenu;
}
public void addChildMenu (Menu childMenu)
{
this.menu.add (childMenu);
}
@JsonProperty("menuID")
public String getMenuID() {
return menuID;
}
@JsonProperty("menuID")
public void setMenuID(String menuID) {
this.menuID = menuID;
}
@JsonProperty("nameKin")
public String getNameKin() {
return nameKin;
}
@JsonProperty("nameKin")
public void setNameKin(String nameKin) {
this.nameKin = nameKin;
}
@JsonProperty("nameEng")
public String getNameEng() {
return nameEng;
}
@JsonProperty("nameEng")
public void setNameEng(String nameEng) {
this.nameEng = nameEng;
}
@JsonProperty("menuStatus")
public String getMenuStatus() {
return menuStatus;
}
@JsonProperty("menuStatus")
public void setMenuStatus(String menuStatus) {
this.menuStatus = menuStatus;
}
@JsonProperty("menuParentID")
public String getMenuParentID() {
return menuParentID;
}
@JsonProperty("menuParentID")
public void setMenuParentID(String menuParentID) {
this.menuParentID = menuParentID;
}
@JsonProperty("lvl")
public Integer getLvl() {
return lvl;
}
@JsonProperty("lvl")
public void setLvl(Integer lvl) {
this.lvl = lvl;
}
@JsonProperty("prioritylist")
public String getPrioritylist() {
return prioritylist;
}
@JsonProperty("prioritylist")
public void setPrioritylist(String prioritylist) {
this.prioritylist = prioritylist;
}
@JsonProperty("quickMenu")
public String getQuickMenu() {
return quickMenu;
}
@JsonProperty("quickMenu")
public void setQuickMenu(String quickMenu) {
this.quickMenu = quickMenu;
}
@JsonProperty("menuType")
public String getMenuType() {
return menuType;
}
@JsonProperty("menuType")
public void setMenuType(String menuType) {
this.menuType = menuType;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
@Override
public String toString() {
return "Menu{" +
"menuID='" + menuID + '\'' +
", nameKin='" + nameKin + '\'' +
", nameEng='" + nameEng + '\'' +
", menuStatus='" + menuStatus + '\'' +
", menuParentID='" + menuParentID + '\'' +
", lvl=" + lvl +
", prioritylist='" + prioritylist + '\'' +
", quickMenu='" + quickMenu + '\'' +
", menuType='" + menuType + '\'' +
", additionalProperties=" + additionalProperties +
", menu=" + menu +
'}';
}
public static Comparator<Menu> COMPARE_BY_MENUID = new Comparator<Menu>() {
@Override
public int compare(Menu menu, Menu menu1) {
return Integer.parseInt(menu.getMenuID()) > Integer.parseInt(menu1.getMenuID()) ? 1 : Integer.parseInt(menu.getMenuID()) < Integer.parseInt(menu1.getMenuID()) ? -1 : 0;
}
};
public static Comparator<Menu> COMPARE_BY_LVL = new Comparator<Menu>() {
@Override
public int compare(Menu menu, Menu menu1) {
int level = menu.getLvl().compareTo(menu1.getLvl());
int menuParentId = menu.getMenuParentID().compareTo(menu1.getMenuParentID());
int priority = menu.getPrioritylist().compareTo(menu1.getPrioritylist());
if (level == 0) {
return ((priority == 0) ? level : menuParentId == 0 ? priority : menuParentId);
} else {
return level;
}
}
};
}
MenuList:
[
Menu {
menuID='1',
nameKin='Guhamagara',
nameEng='Voice',
menuStatus='1',
menuParentID='0',
lvl=1,
prioritylist='1',
quickMenu='',
menuType='Voicepack',
additionalProperties= {
},
menu= [
Menu {
menuID='3',
nameKin='Umunsi',
nameEng='Daily',
menuStatus='1',
menuParentID='1',
lvl=2,
prioritylist='1',
quickMenu='',
menuType='Voicepack',
additionalProperties= {
},
menu= [
Menu {
menuID='11',
nameKin='Yolo Umunsi',
nameEng='Yolo Daily',
menuStatus='1',
menuParentID='3',
lvl=3,
prioritylist='1',
quickMenu='',
menuType='Yolo',
additionalProperties= {
},
menu= [
]
},
Menu {
menuID='17',
nameKin='Irekure Umunsi',
nameEng='Irekure Daily',
menuStatus='1',
menuParentID='3',
lvl=3,
prioritylist='2',
quickMenu='',
menuType='Voicepack',
additionalProperties= {
},
menu= [
]
},
Menu {
menuID='23',
nameKin='Supapack Umunsi',
nameEng='Supapack Daily',
menuStatus='1',
menuParentID='3',
lvl=3,
prioritylist='3',
quickMenu='',
menuType='Voicepack',
additionalProperties= {
},
menu= [
]
},
Menu {
menuID='26',
nameKin='Mumahanga Umunsi',
nameEng='International Daily',
menuStatus='1',
menuParentID='3',
lvl=3,
prioritylist='4',
quickMenu='',
menuType='Voicepack',
additionalProperties= {
},
menu= [
]
}
]
},
Menu {
menuID='4',
nameKin='Icyumweru',
nameEng='Weekly',
menuStatus='1',
menuParentID='1',
lvl=2,
prioritylist='2',
quickMenu='',
menuType='Voicepack',
additionalProperties= {
},
menu= [
Menu {
menuID='12',
nameKin='Yolo Icyumweru',
nameEng='Yolo Weekly',
menuStatus='1',
menuParentID='4',
lvl=3,
prioritylist='1',
quickMenu='',
menuType='Yolo',
additionalProperties= {
},
menu= [
]
},
Menu {
menuID='18',
nameKin='Irekure Icyumweru',
nameEng='Irekure Weekly',
menuStatus='1',
menuParentID='4',
lvl=3,
prioritylist='2',
quickMenu='',
menuType='Voicepack',
additionalProperties= {
},
menu= [
]
},
Menu {
menuID='24',
nameKin='Supapack Icyumweru',
nameEng='Supapack Weekly',
menuStatus='1',
menuParentID='4',
lvl=3,
prioritylist='3',
quickMenu='',
menuType='Voicepack',
additionalProperties= {
},
menu= [
]
},
Menu {
menuID='27',
nameKin='Mumahanga Icyumweru',
nameEng='International Weekly',
menuStatus='1',
menuParentID='4',
lvl=3,
prioritylist='4',
quickMenu='',
menuType='Voicepack',
additionalProperties= {
},
menu= [
]
}
]
},
Menu {
menuID='5',
nameKin='Ukwezi',
nameEng='Monthly',
menuStatus='1',
menuParentID='1',
lvl=2,
prioritylist='3',
quickMenu='',
menuType='Voicepack',
additionalProperties= {
},
menu= [
Menu {
menuID='13',
nameKin='Yolo Ukwezi',
nameEng='Yolo Monthly',
menuStatus='1',
menuParentID='5',
lvl=3,
prioritylist='1',
quickMenu='',
menuType='Yolo',
additionalProperties= {
},
menu= [
]
},
Menu {
menuID='19',
nameKin='Irekure Ukwezi',
nameEng='Irekure Monthly',
menuStatus='1',
menuParentID='5',
lvl=3,
prioritylist='2',
quickMenu='',
menuType='Voicepack',
additionalProperties= {
},
menu= [
]
},
Menu {
menuID='25',
nameKin='Supapack Ukwezi',
nameEng='Supapack Ukwezi',
menuStatus='1',
menuParentID='5',
lvl=3,
prioritylist='3',
quickMenu='',
menuType='Voicepack',
additionalProperties= {
},
menu= [
]
},
Menu {
menuID='28',
nameKin='Mumahanga Ukwezi',
nameEng='International Monthly',
menuStatus='1',
menuParentID='5',
lvl=3,
prioritylist='4',
quickMenu='',
menuType='Voicepack',
additionalProperties= {
},
menu= [
]
},
Menu {
menuID='39',
nameKin='Prestige Ukwezi',
nameEng='Prestige Monthly',
menuStatus='1',
menuParentID='5',
lvl=3,
prioritylist='4',
quickMenu='',
menuType='Voicepack',
additionalProperties= {
},
menu= [
]
}
]
},
Menu {
menuID='9',
nameKin='Weekend',
nameEng='Weekend',
menuStatus='1',
menuParentID='1',
lvl=2,
prioritylist='4',
quickMenu='',
menuType='Voicepack',
additionalProperties= {
},
menu= [
Menu {
menuID='38',
nameKin='Yolo Weekend',
nameEng='Yolo Weekend',
menuStatus='1',
menuParentID='9',
lvl=3,
prioritylist='1',
quickMenu='',
menuType='Yolo',
additionalProperties= {
},
menu= [
]
}
]
}
]
},
Menu {
menuID='2',
nameKin='Interineti',
nameEng='Data',
menuStatus='1',
menuParentID='0',
lvl=1,
prioritylist='2',
quickMenu='',
menuType='Voicepack',
additionalProperties= {
},
menu= [
Menu {
menuID='6',
nameKin='Umunsi',
nameEng='Daily',
menuStatus='1',
menuParentID='2',
lvl=2,
prioritylist='1',
quickMenu='',
menuType='DataBundle',
additionalProperties= {
},
menu= [
Menu {
menuID='14',
nameKin='Yolo Umunsi ',
nameEng='Yolo Daily',
menuStatus='1',
menuParentID='6',
lvl=3,
prioritylist='1',
quickMenu='',
menuType='Yolo',
additionalProperties= {
},
menu= [
]
},
Menu {
menuID='20',
nameKin='Irekure Umunsi',
nameEng='Irekure Daily',
menuStatus='1',
menuParentID='6',
lvl=3,
prioritylist='2',
quickMenu='',
menuType='DataBundle',
additionalProperties= {
},
menu= [
]
},
Menu {
menuID='29',
nameKin='Mumahanga Umunsi',
nameEng='International Daily',
menuStatus='1',
menuParentID='6',
lvl=3,
prioritylist='5',
quickMenu='',
menuType='DataBundle',
additionalProperties= {
},
menu= [
]
},
Menu {
menuID='32',
nameKin='Pack za 3G Umunsi',
nameEng='3G Bundles Daily',
menuStatus='1',
menuParentID='6',
lvl=3,
prioritylist='3',
quickMenu='',
menuType='DataBundle',
additionalProperties= {
},
menu= [
]
},
Menu {
menuID='35',
nameKin='Pack za 4G Umunsi',
nameEng='4G Bundles Daily',
menuStatus='1',
menuParentID='6',
lvl=3,
prioritylist='4',
quickMenu='',
menuType='DataBundle',
additionalProperties= {
},
menu= [
]
}
]
},
Menu {
menuID='7',
nameKin='Icyumweru',
nameEng='Weekly',
menuStatus='1',
menuParentID='2',
lvl=2,
prioritylist='2',
quickMenu='',
menuType='DataBundle',
additionalProperties= {
},
menu= [
Menu {
menuID='15',
nameKin='Yolo Icyumweru',
nameEng='Yolo Icyumweru',
menuStatus='1',
menuParentID='7',
lvl=3,
prioritylist='1',
quickMenu='',
menuType='Yolo',
additionalProperties= {
},
menu= [
]
},
Menu {
menuID='21',
nameKin='Irekure Icyumweru',
nameEng='Irekure Weekly',
menuStatus='1',
menuParentID='7',
lvl=3,
prioritylist='2',
quickMenu='',
menuType='DataBundle',
additionalProperties= {
},
menu= [
]
},
Menu {
menuID='30',
nameKin='Mumahanga Icyumweru',
nameEng='International Weekly',
menuStatus='1',
menuParentID='7',
lvl=3,
prioritylist='5',
quickMenu='',
menuType='DataBundle',
additionalProperties= {
},
menu= [
]
},
Menu {
menuID='33',
nameKin='Pack za 3G Icyumweru',
nameEng='3G Bundles Weekly',
menuStatus='1',
menuParentID='7',
lvl=3,
prioritylist='3',
quickMenu='',
menuType='DataBundle',
additionalProperties= {
},
menu= [
]
},
Menu {
menuID='36',
nameKin='Pack za 4G Icyumweru',
nameEng='4G Bundles Weekly',
menuStatus='1',
menuParentID='7',
lvl=3,
prioritylist='4',
quickMenu='',
menuType='DataBundle',
additionalProperties= {
},
menu= [
]
}
]
},
Menu {
menuID='8',
nameKin='Ukwezi',
nameEng='Ukwezi',
menuStatus='1',
menuParentID='2',
lvl=2,
prioritylist='3',
quickMenu='',
menuType='DataBundle',
additionalProperties= {
},
menu= [
Menu {
menuID='16',
nameKin='Yolo Ukwezi',
nameEng='Yolo Monthly',
menuStatus='1',
menuParentID='8',
lvl=3,
prioritylist='1',
quickMenu='',
menuType='Yolo',
additionalProperties= {
},
menu= [
]
},
Menu {
menuID='22',
nameKin='Irekure Ukwezi',
nameEng='Irekure Monthly',
menuStatus='1',
menuParentID='8',
lvl=3,
prioritylist='3',
quickMenu='',
menuType='DataBundle',
additionalProperties= {
},
menu= [
]
},
Menu {
menuID='31',
nameKin='Mumahanga Ukwezi',
nameEng='International Monthly',
menuStatus='1',
menuParentID='8',
lvl=3,
prioritylist='5',
quickMenu='',
menuType='DataBundle',
additionalProperties= {
},
menu= [
]
},
Menu {
menuID='34',
nameKin='Pack za 3G Ukwezi',
nameEng='3G Bundles Monthly',
menuStatus='1',
menuParentID='8',
lvl=3,
prioritylist='3',
quickMenu='',
menuType='DataBundle',
additionalProperties= {
},
menu= [
]
},
Menu {
menuID='37',
nameKin='Pack za 4G Ukwezi',
nameEng='4G Bundes Monthly',
menuStatus='1',
menuParentID='8',
lvl=3,
prioritylist='4',
quickMenu='',
menuType='DataBundle',
additionalProperties= {
},
menu= [
]
},
Menu {
menuID='40',
nameKin='Prestige Ukwezi',
nameEng='Prestige Monthly',
menuStatus='1',
menuParentID='8',
lvl=3,
prioritylist='6',
quickMenu='',
menuType='DataBundle',
additionalProperties= {
},
menu= [
]
}
]
},
Menu {
menuID='10',
nameKin='Weekend',
nameEng='Weekend',
menuStatus='1',
menuParentID='2',
lvl=2,
prioritylist='4',
quickMenu='',
menuType='DataBundle',
additionalProperties= {
},
menu= [
]
}
]
}
]
输出
如上图中 [4] 所示,我们可以看到Daily
在Weekly
之前和 [8] { {1}}在Weekly
之前重复,这是不正确的。输出应该像