Mincong Huang
Mincong Huang

Reputation: 5552

How to add an element to the end of a seq in scala?

I want to add an element to the end of a seq in scala. But it didn't work. Can somebody help ? Thanks

val data = Seq(
  Vectors.dense(1.0, 2.0),
  Vectors.dense(2.0, 4.0),
  Vectors.dense(3.0, 6.0)
)
data :+ Vectors.dense(4.0, 8.0) // didn't work
println(data)

Result shown
println shows List([1.0,2.0], [2.0, 4.0], [3.0,6.0])

Upvotes: 2

Views: 1904

Answers (1)

Tyth
Tyth

Reputation: 1814

Seq is immutable structure. When you added new element to it, new structure was created and returned, but val "data" remained the same.

Try

val newData = data :+ Vectors.dense(4.0, 8.0)
println(newData)

Upvotes: 7

Related Questions