Scala: what is the best way to append an element to an Array?

后端 未结 3 1734
你的背包
你的背包 2021-01-29 21:34

Say I have an Array[Int] like

val array = Array( 1, 2, 3 )

Now I would like to append an element to the array, say the value

3条回答
  •  悲&欢浪女
    2021-01-29 22:06

    You can use :+ to append element to array and +: to prepend it:

    0 +: array :+ 4
    

    should produce:

    res3: Array[Int] = Array(0, 1, 2, 3, 4)
    

    It's the same as with any other implementation of Seq.

提交回复
热议问题