2013-10-08 62 views
1

我是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) 

缺少什麼我在這裏?

回答

1

缺少hprocessHierarcy(names, this.root) :)

而且,如果修改塊processHierarchy()if(names.size() > 1)避免IndexOutOfBoundsException例外。

+0

謝謝,哇...喝咖啡的時間。 – dwerner