In Scala how to group consecutive elements in array

后端 未结 1 1156
鱼传尺愫
鱼传尺愫 2021-01-21 05:00

Given

scala> val a = (1 to 9).toArray
a: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)

would like to group elements in a in th

相关标签:
1条回答
  • 2021-01-21 05:45

    If you want to get groups by 3 elements you could use method grouped:

    a.grouped(3).toArray
    // Array(Array(1, 2, 3), Array(4, 5, 6), Array(7, 8, 9))
    
    0 讨论(0)
提交回复
热议问题