mkey
mkey

Reputation: 55

How to merge Arrays in Array of Arrays?

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

Answers (2)

jwvh
jwvh

Reputation: 51271

Try this.

myArrays.grouped(2)      //Iterator[Array[Array[Int]]]
        .map(_.flatten)  //Iterator[Array[Int]]
        .toArray         //Array[Array[Int]]

Upvotes: 2

y2k-shubham
y2k-shubham

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

Related Questions