scala“删除”无法正常工作

时间:2013-02-18 23:34:46

标签: scala

根据“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)

2 个答案:

答案 0 :(得分:10)

List在早期版本中有一个删除方法,但它已在2.8中弃用并在2.9中删除。请改用filterNot

答案 1 :(得分:4)

ListBuffer有一个删除方法,但不是List。请参阅here for info on how to idiomatically remove from an immutable List(显然创建一个新列表!)