yuqihengsheng 2012-10-26 12:05
浏览 286
已采纳

拆分字符,组成菜单 求个算法

假使有字符串 :
aa/bb/cc
aa/bb/ff
aa/bb/ee

根据字符的规律,拆分成如下形式的菜单,有没有什么解决方案捏?

aa (一级)
|- bb (二级)
|-cc 。。。。
|-ff
|-ee

考虑字符会很多,具体的结构也不固定,可能会无限N层, 递归 做 ? 存与取是个问题, 求贴码.....

  • 写回答

3条回答 默认 最新

  • fang_fumin 2012-10-26 18:31
    关注

    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;
        }
    
    
    }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度