Reputation: 1071
In this code I'm trying to add values to a Seq[Seq[String]]
structure, but the line shown below throws a compilation error, why is that?
var values2 = Seq[Seq[String]]()
values2 = values2 :+ Seq[String]()
for ( x <- 0 to 5) {
values2(0) = values2(0) :+ (x + "") // <-- value update is not a member of Seq[Seq[String]]
}
Upvotes: 2
Views: 284
Reputation: 253
import scala.collection.mutable
var values2 = mutable.Seq[mutable.Seq[String]]()
values2 = values2 :+ mutable.Seq[String]()
for (x <- 0 to 5) {
values2(0) = values2(0) :+ (x + "")
}
You can solve this by explicitly using mutable.Seq instead of the default Seq, which is immutable.
Upvotes: 1
Reputation: 1054
That's happens because inner collection is immutable and you can't reassign it's value with =
.
But you can use update
method of it (similar to copy
method on case-classes), so it'll be like this:
for ( x <- 0 to 5) {
values2 = values2.updated(0, values2(0) :+ (x + ""))
}
Now you just coping values2
collection with one element changed.
Upvotes: 3