Reputation: 55
I have an Array[Array[Int]]
and what i want to do is, per two of the inside Arrays
, merge their elements to one Array
.
E.g. I have: Array(Array(1), Array(2), Array(3), Array(4))
What i want as a result is:
Array(Array(1, 2) Array(3, 4))
Is something like this possible in scala?
Upvotes: 0
Views: 451
Reputation: 51271
Try this.
myArrays.grouped(2) //Iterator[Array[Array[Int]]]
.map(_.flatten) //Iterator[Array[Int]]
.toArray //Array[Array[Int]]
Upvotes: 2
Reputation: 11607
Try this
val arrArr: Array[Array[Int]] = Array(
Array(1),
Array(2),
Array(3),
Array(4)
)
arrArr.grouped(2).map { l => l.flatten.toArray}.toArray
Upvotes: 0