树数据结构 (GWT) 的字符串路径

发布于 2024-12-17 07:34:46 字数 587 浏览 1 评论 0原文

我的挑战是这样的......我得到一个字符串路径(例如 x1/x2/x3/Xn、Xn/x2/x3 等)(例如 String path = Project.getName() )。所以基本上,我可以有任何类型的路径(任何级别和任何深度)。我正在尝试找到一种方法将其转换为数据结构,以便我可以在我的 gwt CellTree 中实现它。到目前为止,我已经从这个示例实现了 CellTree GWT Cell 树,如何使用?< /a> .

因为,我不知道级别或深度,所以我有点迷失...我想我应该使用递归函数来分割我的字符串路径,然后添加它们,但检查它们是否已经存在,等等...

我还查看了这个问题从字符串列表构造树结构路径。但我对 java 和设计模式有点陌生,所以任何指针或代码位(示例)都会有所帮助。

My challenge is this... I'm getting a string path (e.g x1/x2/x3/Xn, Xn/x2/x3, etc.) (e.g String path = Project.getName() ). So basically, I can have any type of path (any level and any depth). I'm trying to find a way to convert this to a data structure so I can implement it in my gwt CellTree. So far, I have implemented the CellTree from this example GWT Cell tree, how to use? .

Since, I have no idea of the level or the depth, I am a bit lost... I think I should go with a recursive function which split my string path then add them, but check if they already existe, etc...

I also looked over this question Construct a tree structure from list of string paths. But I'm kind of new to java and design patterns, so any pointers or bits of code (example) would help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乙白 2024-12-24 07:34:46
    String fullPath = mystringpath;
    String[] paths = fullPath.split("/");
    String combinedPaths = "";
    Node current = this.root;

    // We will never process the last node since it could be a repository
    // or a wildcard. The last step of this process would be doing that.
    for (int i = 0; i < paths.length - 1; i++) {
        combinedPaths += "/" + paths[i];
        if (this.collected.containsKey(combinedPaths)) {
            current = this.collected.get(combinedPaths);
        }
        else {
            String name = paths[i];
            Node childItem = new Node(name);
            current.addItem(childItem);
            current = childItem;
            this.collected.put(combinedPaths, current);
        }
    }

    // Process the last node since it represents the repository name.
        String name = paths[paths.length - 1];
        Node leafItem = new Node(name);
        combinedPaths += "/" + name;
        current.addItem(leafItem);
        current = leafItem;
        this.collected.put(combinedPaths, current);
    String fullPath = mystringpath;
    String[] paths = fullPath.split("/");
    String combinedPaths = "";
    Node current = this.root;

    // We will never process the last node since it could be a repository
    // or a wildcard. The last step of this process would be doing that.
    for (int i = 0; i < paths.length - 1; i++) {
        combinedPaths += "/" + paths[i];
        if (this.collected.containsKey(combinedPaths)) {
            current = this.collected.get(combinedPaths);
        }
        else {
            String name = paths[i];
            Node childItem = new Node(name);
            current.addItem(childItem);
            current = childItem;
            this.collected.put(combinedPaths, current);
        }
    }

    // Process the last node since it represents the repository name.
        String name = paths[paths.length - 1];
        Node leafItem = new Node(name);
        combinedPaths += "/" + name;
        current.addItem(leafItem);
        current = leafItem;
        this.collected.put(combinedPaths, current);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文