Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append a whole array to an array in Scala? [duplicate]

Tags:

arrays

scala

Say, I have an array with values (1, 2, 3) and another one with (4, 5, 6). How can I have a resultant array with values (1, 2, 3, 4, 5, 6)?

I tried to use ++, but that doesn't work. For example, this is what I got in the command shell.

scala> val x = Array((1, 2, 3))
x: Array[(Int, Int, Int)] = Array((1,2,3))

scala> val y = Array((4, 5, 6))
y: Array[(Int, Int, Int)] = Array((4,5,6))

scala> val z = x ++ y
z: Array[(Int, Int, Int)] = Array((1,2,3), (4,5,6))

Whereas I want Array(1, 2, 3, 4, 5, 6).

EDIT

I was actually using array of tuples, my bad. The Array should have been declared as Array(1, 2, 3) and not Array((1, 2, 3)).

like image 338
MetallicPriest Avatar asked Jan 08 '23 22:01

MetallicPriest


1 Answers

val res = Array(1, 2, 3) ++ Array(4, 5, 6)
like image 104
ka4eli Avatar answered Jan 24 '23 07:01

ka4eli