我是Groovy的新手,但我正在尝试编写一个简单的递归方法来将分层路径解析为对象图。这是我尝试过的:
class Hierarchy {
def root = [:]
static void processHierarchy(names, parent) {
println names
if (names.size() > 0) {
def childName = names[0]
def child = parent[childName]
if (child == null){
child = new Expando()
parent[childName]= child
}
processHierarchy(names[1..-1], child)
}
}
Hierarchy () {
def names = '/some/thing/to/test'.split('/')
if (names != null && names.size() > 0){
processHierarcy(names, this.root)
}
println this.root
}
}
new Hierarchy()
但是我收到以下错误:
Caught: groovy.lang.MissingMethodException: No signature of method: Hierarchy.processHierarcy() is applicable for argument types: ([Ljava.lang.String;, java.util.LinkedHashMap) values: [[, some, thing, to, test], [:]]
Possible solutions: processHierarchy(java.lang.Object, java.lang.Object)
groovy.lang.MissingMethodException: No signature of method: Hierarchy.processHierarcy() is applicable for argument types: ([Ljava.lang.String;, java.util.LinkedHashMap) values: [[, some, thing, to, test], [:]]
Possible solutions: processHierarchy(java.lang.Object, java.lang.Object)
at Hierarchy.<init>(xxx.groovy:48)
at xxx.run(xxx.groovy:54)
我在这里缺少什么?
答案 0 :(得分:1)
在h
中遗漏了processHierarcy(names, this.root)
:)
另外,修改if processHierarchy()
阻止if(names.size() > 1)
以避免IndexOutOfBoundsException
例外。