Loops Groovy中的双循环
如果我有这样一个结构,我如何使用每个循环进行双循环:Loops Groovy中的双循环,loops,groovy,each,Loops,Groovy,Each,如果我有这样一个结构,我如何使用每个循环进行双循环: Termin 1 [ [ 1][2 ][3 ] ] Termin 2 [ [1 ] [2 ] [3] ] Termin.each(){ println("first"); it.each(){ println("second"); // 1 2 3 } } 它在未定义属性名称时使用。您只需更改名称: def nes
Termin 1
[ [ 1][2 ][3 ] ]
Termin 2
[ [1 ] [2 ] [3] ]
Termin.each(){
println("first");
it.each(){
println("second"); // 1 2 3
}
}
它
在未定义属性名称时使用。您只需更改名称:
def nested = [[1],[2],[3]]
nested.each { n ->
n.each { s ->
print "Nested: $s \n"
}
}
更新it
对于包装的闭包是隐式的,因此如果您精通Groovy语义,也可以使用
def nested = [[1],[2],[3]]
nested.each {
// `it` is meant for the nested.each{}
it.each {
// `it` is meant for the it.each{}
print "Nested: $it \n"
}
}
这两种方法都会产生相同的结果。
在不定义属性名称时使用它。您只需更改名称:
def nested = [[1],[2],[3]]
nested.each { n ->
n.each { s ->
print "Nested: $s \n"
}
}
更新
it
对于包装的闭包是隐式的,因此如果您精通Groovy语义,也可以使用
def nested = [[1],[2],[3]]
nested.each {
// `it` is meant for the nested.each{}
it.each {
// `it` is meant for the it.each{}
print "Nested: $it \n"
}
}
这两种方法都会产生相同的结果。我亲爱的朋友,你不必担心它
对包装的闭包是隐式的。@dmahapatro是的,但更改名称会使代码更可读。+1同意。这是为像你这样的极客准备的。无论如何,我已经更新了你的答案,如果可以接受,你可以接受所以,我们都是极客:-)@dmahapatro@Raffian说得对……:)我亲爱的朋友,你不必担心它
对包装的闭包是隐式的。@dmahapatro是的,但更改名称会使代码更可读。+1同意。这是为像你这样的极客准备的。无论如何,我已经更新了你的答案,如果可以接受,你可以接受所以,我们都是极客:-)@dmahapatro@Raffian说得对……:)