Reputation: 1709
As said in the title, I can't reassign a variable of type Arraybuffer(Arraybuffer(Int,Int),Int)
in a for loop
:
var ab1 = ArrayBuffer(le4: _*)
var ab2 = ab1 map (ligne => (ArrayBuffer(ligne._1: _*), ligne._2))
println("ab:" + ab2)
for {
i <- 1 to ab2.length
j <- 0 to i
} {
ab2(i)._1(j)._2 = j match {
case 0 => ab2(i - 1)._1(0)._2 + ab2(i)._1(j)._1
case i => ab2(i - 1)._1(j - 1)._2 + ab2(i)._1(j)._1
case _ => ab2(i - 1)._1(j)._2 + ab2(i - 1)._1(j - 1)._1 + ab2(i)._1(j)._1
}
}
the key point is that ab2
is declared as var but the change of an Int inside it is denied. Why?
Upvotes: 1
Views: 960
Reputation: 13922
There's a difference between a var
, and a mutable
object.
var
can have its value reassigned at willmutable
object can have its fields reassigned. Like an object with var
s in itYou are trying to set the _2
field of a tuple inside of ab2
; tuples are immutable, and that's why it causes a compiler error.
Reconsider the data structure you're using for this operation. A collection.mutable.Map
might be better, or anything else that has an update
method which allows you to change values inside of it.
Upvotes: 6