2016-11-23 66 views
1

我有一個函數,它接受輸入兩個BitSet,然後我在Groovy中調用評估。 由於參數是兩個BitSet,所以我需要將它們初始化爲內聯並通過一個字符串傳遞所有內容。Groovy數組初始化

public static void main(String[] args) { 
    Binding binding = new Binding(); 
    GroovyShell shell = new GroovyShell(binding); 
    path(BitSet.valueOf(new long[] {0b00111111}),BitSet.valueOf(new long[] {0b00000011})); //s3 
    //String s1 = "path(BitSet.valueOf([0b00111111L] as long[]),BitSet.valueOf([0b00111111L] as long[]))"; 
    //String s2 = "path(BitSet.valueOf(long[] o = [0b00111111L]),BitSet.valueOf(long[] oo = [0b00111111L]))"; 
    String s3 = "path(BitSet.valueOf(new long[] {0b00111111}),BitSet.valueOf(new long[] {0b00000011}))"; 
    Object value = shell.evaluate(s3); 


} 

public static LinkedList<BitSet> path(BitSet dstBitSet, BitSet srcBitSet) { 
    LinkedList<BitSet> l = new LinkedList<>(); 
    l.add(srcBitSet); 
    System.out.println("ok"); 
    return l; 
} 

第三線只需要調用使用參數的函數路徑和它的作品。 但是,正是在Groovy同一塊代碼(S3 VAR)是不行的,因爲它返回以下錯誤:

Exception in thread "main" org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: 
Script1.groovy: 1: No expression for the array constructor call at line: 1 column: 29. File: Script1.groovy @ line 1, column 29. 
path(BitSet.valueOf(new long[] {0b00111111}),BitSet.valueOf(new long[]  {0b00000011})) 
          ^

谷歌搜索我發現,Groovy中接受陣列與以下格式inizialitation:

  • [0b00111111]只要[]
  • 長[] O = [0b00111111]

正如你可以通過代碼看到的,我試圖在var s1和s2中做同樣的事情,但仍然無法正常工作。 S1的錯誤是:

Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: Script1.path() is applicable for argument types: (java.util.BitSet, java.util.BitSet) values: [{0, 1, 2, 3, 4, 5}, {0, 1, 2, 3, 4, 5}] 
Possible solutions: wait(), any(), with(groovy.lang.Closure), each(groovy.lang.Closure), run(), run() 

S2的錯誤是:

Exception in thread "main" org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: 
Script1.groovy: 1: Invalid use of declaration inside method call. 

有沒有辦法解決這個問題的方法嗎?

回答

0

試試這個:

path(BitSet.valueOf([0b00111111] as long[]), BitSet.valueOf([0b00111111] as long[]))