java编程两种树形菜单结构的转换代码
程序员文章站
2023-12-04 11:50:22
首先看看两种树形菜单结构的代码示例。
singletreenode:
package com.zzj.tree;
public class singlet...
首先看看两种树形菜单结构的代码示例。
singletreenode:
package com.zzj.tree; public class singletreenode { private int id; private int pid; private string name; public singletreenode() { } public singletreenode(int id, int pid, string name) { this.id = id; this.pid = pid; this.name = name; } public int getid() { return id; } public void setid(int id) { this.id = id; } public int getpid() { return pid; } public void setpid(int pid) { this.pid = pid; } public string getname() { return name; } public void setname(string name) { this.name = name; } @override public string tostring() { return "singletreenode [id=" + id + ", pid=" + pid + ", name=" + name + "]"; } }
这种结构很适合存储到关系型数据库中。
multitreenode:
package com.zzj.tree; import java.util.list; public class multitreenode { private int id; private string name; private list<multitreenode> children; public multitreenode() { } public multitreenode(int id, string name) { this.id = id; this.name = name; } public multitreenode(int id, string name, list<multitreenode> children) { this.id = id; this.name = name; this.children = children; } public int getid() { return id; } public void setid(int id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public list<multitreenode> getchildren() { return children; } public void setchildren(list<multitreenode> children) { this.children = children; } @override public string tostring() { return "multitreenode [id=" + id + ", name=" + name + ", children=" + children + "]"; } }
两种转换器
tomultitreetransformer:
package com.zzj.tree; import java.util.arraylist; import java.util.list; public class tomultitreetransformer { private list<singletreenode> singletreenodes; private list<multitreenode> multitreenodes = new arraylist<>(); public tomultitreetransformer(list<singletreenode> singletreenodes) { this.singletreenodes = singletreenodes; } public list<multitreenode> transform(){ // 先找出所有的根节点 for (int i = 0; i < singletreenodes.size(); i++) { singletreenode singletreenode = singletreenodes.get(i); boolean isroot = true; for (int j = 0; j < singletreenodes.size(); j++) { singletreenode temp = singletreenodes.get(j); if (singletreenode.getpid() == temp.getid()) { isroot = false; break; } } if (isroot) { multitreenode multitreenode = new multitreenode(singletreenode.getid(), singletreenode.getname()); multitreenodes.add(multitreenode); } } // 设置子节点 for (int i = 0; i < multitreenodes.size(); i++) { multitreenode multitreenode = multitreenodes.get(i); setchildren(multitreenode); } return multitreenodes; } /** * 设置子节点 * @param multitreenode */ private void setchildren(multitreenode multitreenode){ for (int j = 0; j < singletreenodes.size(); j++) { singletreenode temp = singletreenodes.get(j); if (temp.getpid() == multitreenode.getid()) { multitreenode child = new multitreenode(temp.getid(), temp.getname()); list<multitreenode> children = multitreenode.getchildren(); if (children == null) { children = new arraylist<>(); multitreenode.setchildren(children); } children.add(child); setchildren(child); } } } }
tosingletreetransformer:
package com.zzj.tree; import java.util.arraylist; import java.util.list; public class tosingletreetransformer { private list<multitreenode> multitreenodes; private list<singletreenode> singletreenodes = new arraylist<>(); public tosingletreetransformer(list<multitreenode> multitreenodes) { this.multitreenodes = multitreenodes; } public list<singletreenode> transform(){ // 先获取根节点 for (int i = 0; i < multitreenodes.size(); i++) { multitreenode multitreenode = multitreenodes.get(i); singletreenode singletreenode = new singletreenode(multitreenode.getid(), 0, multitreenode.getname()); singletreenodes.add(singletreenode); } // 获取子节点 for (int i = 0; i < multitreenodes.size(); i++) { multitreenode multitreenode = multitreenodes.get(i); getchildren(multitreenode); } return singletreenodes; } /** * 获取子节点 * @param multitreenode */ private void getchildren(multitreenode multitreenode){ for (int i = 0; i < singletreenodes.size(); i++) { singletreenode temp = singletreenodes.get(i); if (multitreenode.getid() == temp.getid()) { list<multitreenode> children = multitreenode.getchildren(); if (children != null) { for (int j = 0; j < children.size(); j++) { multitreenode child = children.get(j); singletreenode singletreenode = new singletreenode(child.getid(), multitreenode.getid(), child.getname()); singletreenodes.add(singletreenode); getchildren(child); } } } } } }
测试
package com.zzj.tree; import java.util.arraylist; import java.util.list; public class treetransformertest { private final static list<singletreenode> single_tree = new arraylist<singletreenode>(); static { singletreenode china = new singletreenode(1, 0, "中国"); single_tree.add(china); singletreenode hunan = new singletreenode(2, 1, "湖南"); single_tree.add(hunan); singletreenode changsha = new singletreenode(3, 2, "长沙"); single_tree.add(changsha); singletreenode hubei = new singletreenode(4, 1, "湖北"); single_tree.add(hubei); singletreenode wuhan = new singletreenode(5, 4, "武汉"); single_tree.add(wuhan); singletreenode america = new singletreenode(6, 0, "美国"); single_tree.add(america); singletreenode california = new singletreenode(7, 6, "加利福尼亚"); single_tree.add(california); singletreenode losangeles = new singletreenode(8, 7, "洛杉矶"); single_tree.add(losangeles); } public static void main(string[] args) throws exception { tomultitreetransformer multitreetransformer = new tomultitreetransformer(single_tree); list<multitreenode> multitreenodes = multitreetransformer.transform(); system.out.println(multitreenodes); tosingletreetransformer singletreetransformer = new tosingletreetransformer(multitreenodes); list<singletreenode> singletreenodes = singletreetransformer.transform(); system.out.println(singletreenodes); } }
输出结果:
[multitreenode [id=1, name=中国, children=[multitreenode [id=2, name=湖南, children=[multitreenode [id=3, name=长沙, children=null]]], multitreenode [id=4, name=湖北, children=[multitreenode [id=5, name=武汉, children=null]]]]], multitreenode [id=6, name=美国, children=[multitreenode [id=7, name=加利福尼亚, children=[multitreenode [id=8, name=洛杉矶, children=null]]]]]] [singletreenode [id=1, pid=0, name=中国], singletreenode [id=6, pid=0, name=美国], singletreenode [id=2, pid=1, name=湖南], singletreenode [id=3, pid=2, name=长沙], singletreenode [id=4, pid=1, name=湖北], singletreenode [id=5, pid=4, name=武汉], singletreenode [id=7, pid=6, name=加利福尼亚], singletreenode [id=8, pid=7, name=洛杉矶]]
总结
以上就是本文关于java编程两种树形菜单结构的转换代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:
如有不足之处,欢迎留言指出!