a.moussa a.moussa - 20 days ago 5
Scala Question

Concat two Scala Array

I have two Array like this:

val l1 = Array((1,2,3), (6,2,-3), (6,2,-4))
val l2 = Array("a","b","c")


I would like to put values of l2 in array at the same position in l1 and obtain a final array like that

Array((1,2,3,"a"), (6,2,-3,"b"), (6,2,-4,"c"))


I was thinking about something like:

val l3 = l1.map( code...)


But i don't know how to iterate on l2 during map on l1.

Do you have any idea?

Answer

Combining collections in this way can be done with Zipping.

l1.zip(l2).map{ case (x,y) => (x._1, x._2, x._3, y) }
Comments