Scala prepend +:op不修改ListBufffer

Shr*_*kar 1 scala prepend listbuffer

我在listbuffer类型上使用prepend方法并观察一些奇怪的行为.prepend操作返回一个可接受的新列表.但是它不应该修改ListBuffer吗?在预先添加之后,我仍然看到ListBuffer的长度没有改变.我在这里错过了什么吗?

scala> val buf = new ListBuffer[Int]
buf: scala.collection.mutable.ListBuffer[Int] = ListBuffer()

scala> buf += 1                 
res47: buf.type = ListBuffer(1)

scala>  buf += 2
res48: buf.type = ListBuffer(1, 2)

scala> 3 +: buf
res49: scala.collection.mutable.ListBuffer[Int] = ListBuffer(3, 1, 2)

scala> buf.toList
res50: List[Int] = List(1, 2)
Run Code Online (Sandbox Code Playgroud)

1es*_*sha 11

使用 +=:

scala> val buf = new ListBuffer[Int]
buf: scala.collection.mutable.ListBuffer[Int] = ListBuffer()

scala> buf += 1
res0: buf.type = ListBuffer(1)

scala> buf += 2
res1: buf.type = ListBuffer(1, 2)

scala> 3 +=: buf
res2: buf.type = ListBuffer(3, 1, 2)

scala> buf.toList
res3: List[Int] = List(3, 1, 2)
Run Code Online (Sandbox Code Playgroud)