根据“Scala编程”一书的第44页,remove
数据结构存在list
函数。但是,当我在我的翻译中尝试这个例子时,我不断收到错误。有谁知道为什么?这是一个样本
scala> val x = List(1,2,3,4,5,6,7,8,9)
x: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> x.remove(_ < 5)
<console>:9: error: value remove is not a member of List[Int]
x.remove(_ < 5)
^
scala> x.remove(s => s == 5)
<console>:9: error: value remove is not a member of List[Int]
x.remove(s => s == 5)
^
scala> val y = List("apple","Oranges","pine","sol")
y: List[String] = List(apple, Oranges, pine, sol)
scala> y.remove(s => s.length ==4)
<console>:9: error: value remove is not a member of List[String]
y.remove(s => s.length ==4)
答案 0 :(得分:10)
List
在早期版本中有一个删除方法,但它已在2.8中弃用并在2.9中删除。请改用filterNot
。
答案 1 :(得分:4)
ListBuffer有一个删除方法,但不是List。请参阅here for info on how to idiomatically remove from an immutable List(显然创建一个新列表!)