Groovy二维数组 [英] Groovy 2d arrays

查看:1432
本文介绍了Groovy二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个数组, l1 [,,,,],l2 [,,,,]和l3 [,,,,] 。每一个都有5个字符。

 例如。 l1 [A,B,C,D,E] 

2d数组是由这些元素组成的

$ p $ screen = [l1 [],l2 [],l3 []]

所以它看起来是这样的:

  screen = [[,,,,],[,,,,],[,,,,]] 

我如何迭代这个数组?



我是否调用 screen [5] ?或屏幕[l1 [5]]



我可以:

  for(i in 1..15){

println screen [i]
}


$ b $ p请帮忙!!!

如何迭代数组并调用不同的部分,例如每个子数组中的 1st元素



谢谢

 列表l1 = ['A','B ','C','D','E'] 
List l2 = ['a','b','c','d','e']
List l3 = [' 1,2,3,4,5]

/ pre>

要迭代所有元素,您可以这样做:

  3times {y  - > 
5.times {x - >
println screen [y] [x]
}
}

或者,您可以使用 mod intdiv 来计算x和y的位置:

  15.times {p  - > 
def x = p%5
def y = p.intdiv(5)
println screen [y] [x]
}

或者,如果您想要每个子列表中的第一个元素,您可以这样做:

  println screen * .head()

您需要每个子列表中的第三个元素:

  println screen * .getAt(2)

或者,您可以将所有项目放入一维列表中:

  def inlineScreen = [* l1,* l2,* l3] 



然后通过访问:

  15.times {p  - > 
println inlineScreen [p]
}

或:

  def x = 1,y = 1 
println inlineScreen [x + y * 5]

或者,如果总是希望将所有元素放在一起(即,总是希望所有数组中的第二个元素放在一起),那么重要的是,那么你可以这样做:

  // groupedScreen == [['A','a','1'],[ 'B','b','2'],['C','c','3'],['D','d','4'],['E','e' '5']] 
def groupedScreen = [l1,l2,l3] .transpose()

然后,获得第三个元素,你可以这样做:

  groupedScreen [2] 


I have 3 arrays, l1[,,,,], l2[,,,,] and l3[,,,,]. Each of which has 5 characters in it.

e.g. l1["A","B","C","D","E"]

The 2d array is made up of these

screen = [l1[],l2[],l3[]]

so it will look as so:

screen = [[,,,,],[,,,,],[,,,,]]

How can I iterate through this array?

do I call screen[5]? or screen[l1[5]]?

can I:

for (i in 1..15){

 println screen[i]
}

please help!!!

How can I iterate in the array and call different parts, e.g. the 1st element of each sub array?

Thanks

解决方案

So, given:

List l1 = ['A', 'B', 'C', 'D', 'E']
List l2 = ['a', 'b', 'c', 'd', 'e']
List l3 = ['1', '2', '3', '4', '5']

List screen = [ l1, l2, l3 ]

To iterate all the elements, you could do:

3.times { y ->
    5.times { x ->
        println screen[ y ][ x ]  
    }
}

Or, you could use mod and intdiv to work out x and y positions:

15.times { p ->
    def x = p % 5
    def y = p.intdiv( 5 )
    println screen[ y ][ x ]
}

Or, if you want the first element from each sub-list, you can do:

println screen*.head()

Or, say you want the 3rd element from each sub-list:

println screen*.getAt( 2 )

Alternatively, you could put all the items into a one-dimensional List:

def inlineScreen = [ *l1, *l2, *l3 ]

Then access then by:

15.times { p ->
    println inlineScreen[ p ]
}

or:

def x = 1, y = 1
println inlineScreen[ x + y * 5 ]

Or, if it is important that you always want to get the elements together (ie, you always want the second elements from all the arrays together), then you could do:

// groupedScreen == [['A', 'a', '1'], ['B', 'b', '2'], ['C', 'c', '3'], ['D', 'd', '4'], ['E', 'e', '5']]
def groupedScreen = [ l1, l2, l3 ].transpose()

Then, to get the 3rd elements, you can just do:

groupedScreen[ 2 ]

这篇关于Groovy二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆