我已经定义了数组
def sample1 = ["A","B","C","D"] as String[]
..
..
def sample9 = ["555","454","678","456"] as String[]
def p = ["1","2","3","4"] as String[]
for (k=0; k <= 4; k++) {
setValues(sample1[k].concat(p[k]), sample9[k])
} `
我正在尝试获取类似的值:
A1 = 555
B2 = 454
但在执行时我收到错误:
groovy.lang.MissingMethodException: No signature of method: Script7.setValues() is applicable for argument types: (java.lang.String, java.lang.String) values: [A1, [555]] Possible solutions: getClass() error at line: XX
有人可以帮忙吗?我们可以将1个数组的值设置为另一个吗?如果是这样,请帮我解决这个问题
答案 0 :(得分:1)
您可以合并列表,转置它们,然后使用collectEntries
创建地图。 e.g。
def sample1 = ["A","B","C","D"]
def sample9 = ["555","454","678","456"]
def p = ["1","2","3","4"]
println([sample1, p, sample9].transpose().collectEntries{ k1, k2, v -> ["${k1}${k2}", v] })
// -> [A1:555, B2:454, C3:678, D4:456]