2013-11-28 42 views
0

我有一個問題,爲什麼使用收集到字符串數組不工作。收集可能的def隊列= []作爲字符串[]?

這是工作確定

String[][] serversAndQueues = [["server1","queue"],["server1","queue1"],["server2","queue2"]] 

serversAndQueues.groupBy { it[ 0 ] }.each { server, value -> 
    def queues = [] 
    value.collect(queues){ 
     it[1] 
    } 
    println "$server => $queues" 
} 

OUTPUT

server1 => [queue, queue1] 
server2 => [queue2] 

但如果我嘗試定義隊列作爲一個字符串數組DEF隊列= []作爲字符串[],例外情況

String[][] serversAndQueues = [["server1","queue"],["server1","queue1"],["server2","queue2"]] 

serversAndQueues.groupBy { it[ 0 ] }.each { server, value -> 
    def queues = [] as String[] 
    value.collect(queues){ 
     it[1] 
    } 
    println "$server => $queues" 
} 

輸出

Exception thrown 

    groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.collect() is applicable for argument types: ([Ljava.lang.String;, ConsoleScript40$_run_closure2_closure3) values: [[], [email protected]] 
    Possible solutions: collect(), collect(), collect(groovy.lang.Closure), collect(java.util.Collection, groovy.lang.Closure), collect(java.util.Collection, groovy.lang.Closure), collect(groovy.lang.Closure) 

     at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55) 

     at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:46) 
    .. 
    ... 
+0

中所提供的答案任何幫助嗎? –

回答

1

無法收集到一個數組中,只有集合

你可以嘗試

def queues = value.collect { it[ 1 ] } as String[]