JTree 搜索递归问题

发布于 2024-12-09 04:21:46 字数 1063 浏览 0 评论 0原文

我是 JTree 的新手,所以如果我忽略了一些非常基本的事情,请耐心等待。我正在开发 Jtree 文件浏览器。它需要一个目录、添加节点、选择节点以及所有这些好东西。基础知识已经完成。额外的实现包括给出所选节点的文件/文件夹大小,这非常简单。但问题在于试图搜索树。它识别出该文件存在。但由于某种原因递归正在重复???并且该值没有正确返回。我把这两种方法都列出来供参考。如果有与此相关的阅读,也将不胜感激。

public void searchTree(String find)
{
    TreePath root = tree.getPathForRow(0);

    System.out.println(search (root, find));
}


public String search (TreePath path, String find)
{
    TreeNode currentNode = (TreeNode)path.getLastPathComponent();

    String findPath = null;

    if (currentNode.isLeaf() && currentNode.toString().startsWith(find))
    {
            findPath = path.getPath()[path.getPath().length-2].toString() + File.separator + path.getPath()[path.getPath().length-1].toString();
            return findPath;  
    }

    if (!currentNode.isLeaf() && currentNode.getChildCount()> 0)
        for (int i = 0; i < currentNode.getChildCount(); i++)
                search(path.pathByAddingChild(currentNode.getChildAt(i)), find);

    return findPath;
}

更准确地说,搜索输出出现了两次。我做错了什么???

I am new to using JTree, so please bear with me if I am overlooking some very basic things. I am working on a Jtree file explorer. It takes a directory, adds nodes, select nodes and all that good stuff. The basics are done. The extra implementation includes giving the file/folder size of the selected node, which was pretty straightforward. But the problem lies in trying to search the tree. It recognizes that the file is present. But the recursion is repeating itself for some reason??? And the value is not returning properly. I've included the two methods for reference. If there are reading relating to this, it will also be greatly appreciated.

public void searchTree(String find)
{
    TreePath root = tree.getPathForRow(0);

    System.out.println(search (root, find));
}


public String search (TreePath path, String find)
{
    TreeNode currentNode = (TreeNode)path.getLastPathComponent();

    String findPath = null;

    if (currentNode.isLeaf() && currentNode.toString().startsWith(find))
    {
            findPath = path.getPath()[path.getPath().length-2].toString() + File.separator + path.getPath()[path.getPath().length-1].toString();
            return findPath;  
    }

    if (!currentNode.isLeaf() && currentNode.getChildCount()> 0)
        for (int i = 0; i < currentNode.getChildCount(); i++)
                search(path.pathByAddingChild(currentNode.getChildAt(i)), find);

    return findPath;
}

To be more exact, the search output is appearing twice. What am I doing wrong???

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

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

发布评论

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

评论(1

勿挽旧人 2024-12-16 04:21:46

所以我意识到我有点睡眠不足,并意识到该方法在树的初始创建过程中被调用了两次。但不知道为什么这个方法被调用两次。但我稍微调整了代码,一切就恢复正常了。

So I realized that I was kinda sleep deprived and realized that the method was called twice by the initial creation of the tree. Don't know why this method was called twice though. But i tweaked the code a little and things are back to normal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文