0
我想知道,爲什麼這不起作用:錯誤添加元素可變序列
import scala.collection.mutable
var array: mutable.Seq[Int] = mutable.ArrayBuffer[Int]()
array += 5
我得到一個錯誤,指出只有+=
作品的字符串,這是爲什麼?
error: value += is not a member of scala.collection.mutable.Seq[Int]
Expression does not convert to assignment because:
type mismatch;
found : Int(5)
required: String
expansion: array = array.$plus(5)
array += 5
^
[在Scala中將元素添加到Seq \ [String \]中](https://stackoverflow.com/questions/26579853/add-element-to-seqstring-in-scala) – eje
使用'var'和一個可變的集合是衝突的。最好將可變集合聲明爲'val'。 – cchantep
@cchantep如果我這樣做,我將無法改變集合...''表達式不會轉換爲賦值,因爲接收器是不可分配的'' –