Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swing JTree搜索递归问题_Swing_Search_Recursion_Jtree - Fatal编程技术网

Swing JTree搜索递归问题

Swing JTree搜索递归问题,swing,search,recursion,jtree,Swing,Search,Recursion,Jtree,我对使用JTree还不熟悉,所以如果我忽略了一些非常基本的东西,请原谅我。我正在使用Jtree文件资源管理器。它需要一个目录,添加节点,选择节点和所有好的东西。基本工作已经完成。额外的实现包括给出所选节点的文件/文件夹大小,这非常简单。但问题在于试图搜索树。它可以识别文件是否存在。但是递归由于某种原因在重复自己???并且该值没有正确返回。我已经包括了这两种方法以供参考。如果有与此相关的阅读,也将不胜感激 public void searchTree(String find) { Tree

我对使用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;
}
publicsvoidsearchtree(字符串查找)
{
TreePath root=tree.getPathForRow(0);
System.out.println(搜索(root,find));
}
公共字符串搜索(树路径,字符串查找)
{
TreeNode currentNode=(TreeNode)路径。getLastPathComponent();
字符串findPath=null;
if(currentNode.isLeaf()&¤tNode.toString().startsWith(find))
{
findPath=path.getPath()[path.getPath().length-2].toString()+File.separator+path.getPath()[path.getPath().length-1].toString();
返回findPath;
}
如果(!currentNode.isLeaf()&¤tNode.getChildCount()>0)
对于(int i=0;i

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

所以我意识到我有点睡眠不足,并意识到该方法在最初创建树时被调用了两次。但是我不知道为什么这个方法被调用了两次。但是我对代码做了一些调整,一切都恢复正常了