在 Scala 中从 MutableList 中删除一个元素
Removing an element from MutableList in Scala
我有一个 MutableList
,我想从中删除一个元素,但我找不到合适的方法。有一种方法可以像这样从 ListBuffer
中删除元素:
val x = ListBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9)
x -= 5
我无法在 MutableList
上找到等效方法。
MutableList
缺少 -=
和 --=
,因为它没有扩展 Shrinkable
trait. Various motivations for this can be found here。
MutableList
确实有 diff
、filter
和其他方法可以帮助您,以防您处于重新分配变量(或实例化新变量)可能的情况是一种选择,性能问题不是最重要的:
var mylist = MutableList(1, 2, 3)
mylist = mylist diff Seq(1)
val myNewList = mylist.filter(_ != 2)
val indexFiltered = mylist.zipWithIndex.collect { case (el, ind) if ind != 1 => el }
您可以经常使用 ListBuffer
而不是 MutableList
,这将解锁所需的 -=
和 --=
方法:
val mylist = ListBuffer(1, 2, 3)
mylist -= 1 //mylist is now ListBuffer(2, 3)
mylist --= Seq(2, 3) //mylist is now empty
这不是答案,只是为了警告你问题(至少在 2.11.x):
//street magic
scala> val a = mutable.MutableList(1,2,3)
a: scala.collection.mutable.MutableList[Int] = MutableList(1, 2, 3)
scala> a += 4
res7: a.type = MutableList(1, 2, 3, 4)
scala> a
res8: scala.collection.mutable.MutableList[Int] = MutableList(1, 2, 3, 4)
scala> a ++= List(8,9,10)
res9: a.type = MutableList(1, 2, 3, 4, 8, 9, 10)
scala> val b = a.tail
b: scala.collection.mutable.MutableList[Int] = MutableList(2, 3, 4, 8, 9, 10)
scala> b.length
res10: Int = 6
scala> a.length
res11: Int = 7
scala> a ++= List(8,9,10)
res12: a.type = MutableList(1, 2, 3, 4, 8, 9, 10, 8, 9, 10)
scala> b += 7
res13: b.type = MutableList(2, 3, 4, 8, 9, 10, 7)
scala> a
res14: scala.collection.mutable.MutableList[Int] = MutableList(1, 2, 3, 4, 8, 9, 10, 7)
scala> b
res15: scala.collection.mutable.MutableList[Int] = MutableList(2, 3, 4, 8, 9, 10, 7)
scala> a ++= List(8,9,10)
res16: a.type = MutableList(1, 2, 3, 4, 8, 9, 10, 7)
这个例子摘自一些要点——我已经用#devid_blein #street_magic 标签将它发布在 facebook 上,但在互联网上找不到原始的 link。
我有一个 MutableList
,我想从中删除一个元素,但我找不到合适的方法。有一种方法可以像这样从 ListBuffer
中删除元素:
val x = ListBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9)
x -= 5
我无法在 MutableList
上找到等效方法。
MutableList
缺少 -=
和 --=
,因为它没有扩展 Shrinkable
trait. Various motivations for this can be found here。
MutableList
确实有 diff
、filter
和其他方法可以帮助您,以防您处于重新分配变量(或实例化新变量)可能的情况是一种选择,性能问题不是最重要的:
var mylist = MutableList(1, 2, 3)
mylist = mylist diff Seq(1)
val myNewList = mylist.filter(_ != 2)
val indexFiltered = mylist.zipWithIndex.collect { case (el, ind) if ind != 1 => el }
您可以经常使用 ListBuffer
而不是 MutableList
,这将解锁所需的 -=
和 --=
方法:
val mylist = ListBuffer(1, 2, 3)
mylist -= 1 //mylist is now ListBuffer(2, 3)
mylist --= Seq(2, 3) //mylist is now empty
这不是答案,只是为了警告你问题(至少在 2.11.x):
//street magic
scala> val a = mutable.MutableList(1,2,3)
a: scala.collection.mutable.MutableList[Int] = MutableList(1, 2, 3)
scala> a += 4
res7: a.type = MutableList(1, 2, 3, 4)
scala> a
res8: scala.collection.mutable.MutableList[Int] = MutableList(1, 2, 3, 4)
scala> a ++= List(8,9,10)
res9: a.type = MutableList(1, 2, 3, 4, 8, 9, 10)
scala> val b = a.tail
b: scala.collection.mutable.MutableList[Int] = MutableList(2, 3, 4, 8, 9, 10)
scala> b.length
res10: Int = 6
scala> a.length
res11: Int = 7
scala> a ++= List(8,9,10)
res12: a.type = MutableList(1, 2, 3, 4, 8, 9, 10, 8, 9, 10)
scala> b += 7
res13: b.type = MutableList(2, 3, 4, 8, 9, 10, 7)
scala> a
res14: scala.collection.mutable.MutableList[Int] = MutableList(1, 2, 3, 4, 8, 9, 10, 7)
scala> b
res15: scala.collection.mutable.MutableList[Int] = MutableList(2, 3, 4, 8, 9, 10, 7)
scala> a ++= List(8,9,10)
res16: a.type = MutableList(1, 2, 3, 4, 8, 9, 10, 7)
这个例子摘自一些要点——我已经用#devid_blein #street_magic 标签将它发布在 facebook 上,但在互联网上找不到原始的 link。