0

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?

2 Answers 2

2

Try this.

myArrays.grouped(2)      //Iterator[Array[Array[Int]]]
        .map(_.flatten)  //Iterator[Array[Int]]
        .toArray         //Array[Array[Int]]
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, i didn't know grouped. That works like magic, thanks :)
0

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

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.