简体   繁体   English

使用递归函数构建地图

[英]Build Map Using Recursive Function

The idea of the function is that it will build a map that contains a key (path) and the data corresponding to that path. 该函数的想法是,它将构建一个包含键(路径)和与该路径相对应的数据的映射。 This is why I keep passing the map back as it's being built. 这就是为什么我在构建地图时不断将地图传回来的原因。

The problem seems to be that it gets to the point where there are no children, yet still appends the next path onto the current path: 问题似乎是它到达了没有子节点的地步,但仍将下一个路径附加到当前路径上:

Input for the path will always start with "/". 路径的输入将始终以“ /”开头。 We get the children for this, which may be level_1A, level_1C, level_1B. 我们为此得到了孩子,可能是level_1A,level_1C,level_1B。 Then I recurse on each of these to see if they have children. 然后,我递归查看它们是否有孩子。

Assume that level_1A has children level_2A, level_2B. 假设级别_1A的子级为级别_2A,即级别_2B。 Somehow, the algorithm gets caught up and is appending like so: 不知何故,算法陷入了困境,并像这样追加:

/level_1A/level_2B/level_2A

Whereas, it should be treating these seperately, like this: 鉴于它应该分开处理,如下所示:

/level_1A/level_2A
/level_1A/level_2B

Here's what the entire structure would look like: 整个结构如下所示:

/level_1A            data_1A
/level_1A/level_2A   data_2A
/level_1A/level_2B   data_2B

/level_1B            (empty)

/level_1C            (empty)

Here's the recursive method: 这是递归方法:

public Map<String, String> getAll(String path, Map<String, String> all) throws Exception {

        List<String> children = client.getChildren().forPath(path);

        if(children != null && children.size() > 0) {

            for(String child: children) {
                                                                                                    System.out.println("child: " + child);
                if(!path.equals("/")) { 
                    path = path + "/" +  child;
                } else {
                    path = path + child;
                }

                Stat stat = client.checkExists().watched().forPath(path);

                if(stat != null && stat.getDataLength() > 0) {
                    all.put(path, new String(client.getData().forPath(path)));
                }

                getAll(path, all);
            }

        } 

    return all;
}

The error is here: 错误在这里:

 for(String child: children) {           
     if(!path.equals("/")) { 
         path = path + "/" +  child;
     } else {
         path = path + child;
     }
     ...
 }

path variable is out of for loop scope, so in the first iteration you've modified path variable and in the second iteration that modified value is been concatenated with second child and then been passed deeper. path变量不在for loop范围内,因此在第一次迭代中,您已经修改了path变量,在第二次迭代中,已修改的值与第二个子级连接在一起,然后被更深地传递。

So Just provide for-loop scoped variable and use it in iteration: 因此,只需提供for循环作用域变量并在迭代中使用它即可:

 for(String child: children) {
     String pathChild = path;           
     if(!path.equals("/")) { 
         pathChild = path + "/" +  child;
     } else {
         pathChild = path + child;
     }
     //pathChild is used below
     ...
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM