如何在Groovy循环中从映射中分配多个值。

huangapple go评论47阅读模式
英文:

How to assign multiple values from a map in a loop in Groovy

问题

在Groovy中,您可以使用each循环来处理这个地图,并且可以通过keyvalue来访问地图的内容。以下是修改后的代码示例:

def routemap = [ One: ['Berlin','Hamburg'], Two: ['London','Paris'], Three: ['Rome','Barcelona']]

routemap.each { route, startend ->
    println "Route: $route"
    println "Start: ${startend[0]} End: ${startend[1]}"
}

这个代码会输出您期望的结果:

Route: One
Start: Berlin End: Hamburg
Route: Two
Start: London End: Paris
Route: Three
Start: Rome End: Barcelona

这样就可以正确地访问每个地图项的值了。如果您有任何其他问题,请随时提问。

英文:

I have a map in groovy where each item has multiple values that I need to use in a loop. The number of values is fixed, so could be stored in a list/array, tuple - does not matter.

Example, I tried:

def routemap = [ One: ['Berlin','Hamburg'], Two: ['London','Paris'], Three: ['Rome','Barcelona']]

routemap.each { route, startend ->
    println "Route: $route"
    println "Start: $startend[0] End: $startend[1]"
}

This does not provide the desired result, the list index does not work.

Route: One
Start: [Berlin, Hamburg][0] End: [Berlin, Hamburg][1]
Route: Two
Start: [London, Paris][0] End: [London, Paris][1]
Route: Three
Start: [Rome, Barcelona][0] End: [Rome, Barcelona][1]

Also I am wondering if there is a better, more elegant way to do it? Is there a way to assign two values (or a pair or tuple like in Python) instead of "startend" in the loop?

答案1

得分: 1

你应该将 $startend[index] 放在大括号内,就像 ${startend[0]} 一样。所以你的代码会看起来像这样:

def routemap = [ One: ['Berlin','Hamburg'], Two: ['London','Paris'], Three: ['Rome','Barcelona']]

routemap.each { route, startend ->
    println "Route: $route"
    println "Start: ${startend[0]} End: ${startend[1]}" // 这里是修改的部分
}
英文:

You should put $startend[index] in the curly brackets, like ${startend[0]}. So your code will look like that:

def routemap = [ One: ['Berlin','Hamburg'], Two: ['London','Paris'], Three: ['Rome','Barcelona']]

routemap.each { route, startend ->
    println "Route: $route"
    println "Start: ${startend[0]} End: ${startend[1]}" // here is the change
}

答案2

得分: 1

你可以在startend参数上使用多重赋值:

def routemap = [ One: ['柏林', '汉堡'], Two: ['伦敦', '巴黎'], Three: ['罗马', '巴塞罗那']]

routemap.each{ route, startend ->
  def (start, end) = startend
  println "路线: $route"
  println "起点: $start 终点: $end"
}

输出:

路线: One
起点: 柏林 终点: 汉堡
路线: Two
起点: 伦敦 终点: 巴黎
路线: Three
起点: 罗马 终点: 巴塞罗那
英文:

You can literally use the multiple assignment for the startend argument:

def routemap = [ One: ['Berlin','Hamburg'], Two: ['London','Paris'], Three: ['Rome','Barcelona']]

routemap.each{ route, startend ->
  def ( start, end ) = startend
  println "Route: $route"
  println "Start: $start End: $end"
}

prints

Route: One
Start: Berlin End: Hamburg
Route: Two
Start: London End: Paris
Route: Three
Start: Rome End: Barcelona

huangapple
  • 本文由 发表于 2023年5月10日 16:31:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定