假使有字符串 :
aa/bb/cc
aa/bb/ff
aa/bb/ee
根据字符的规律,拆分成如下形式的菜单,有没有什么解决方案捏?
aa (一级)
|- bb (二级)
|-cc 。。。。
|-ff
|-ee
考虑字符会很多,具体的结构也不固定,可能会无限N层, 递归 做 ? 存与取是个问题, 求贴码.....
public class Test
{
public void parseString()
{
NodTree nodeTree = new NodTree();
nodeTree.setNodeValue("root");
String str = "aa/bb/cc/dd";
String str1 = "aa/bb/cc/ee";
String str2 = "aa/bb/cc/ff";
String str3 = "bb/cc/ff";
addNode(nodeTree,str.split("/"));
addNode(nodeTree,str1.split("/"));
addNode(nodeTree,str2.split("/"));
addNode(nodeTree,str3.split("/"));
print(nodeTree,0);
System.out.println("1111111");
}
public void print(NodTree nodeTree,int w)
{
for(int i=0;i<w;i++)
{
System.out.print(" ");
}
System.out.println("|-"+nodeTree.nodeValue);
for(NodTree tree:nodeTree.getNodeList())
{
print(tree,w+1);
}
}
public void addNode(NodTree nodeTree, String[] strArr)
{
NodTree creatNodeTree = null;
NodTree queryNode = null;
NodTree parentNode = nodeTree;
for(String str:strArr)
{
queryNode = parentNode.hasSubNode(str);
if(null == queryNode)
{
creatNodeTree = new NodTree();
creatNodeTree.setNodeValue(str);
parentNode.addSunNode(creatNodeTree);
parentNode =creatNodeTree;
}
else
{
parentNode = queryNode;
}
}
}
public static void main(String[] args)
{
Test t = new Test();
t.parseString();
}
class NodTree
{
String nodeValue = null;
List<NodTree> nodeList = new ArrayList<Test.NodTree>();
public String getNodeValue()
{
return nodeValue;
}
public void setNodeValue(String nodeValue)
{
this.nodeValue = nodeValue;
}
public List<NodTree> getNodeList()
{
return nodeList;
}
public void setNodeList(List<NodTree> nodeList)
{
this.nodeList = nodeList;
}
public void addSunNode(NodTree nodeTree)
{
nodeList.add(nodeTree);
}
public NodTree hasSubNode(String str)
{
if(null !=nodeList && !nodeList.isEmpty() )
{
for(NodTree nodeTree:nodeList)
{
if(nodeTree.nodeValue.equals(str))
{
return nodeTree;
}
}
}
return null;
}
}
}