2013-02-18 141 views
4

根据“Scala编程”一书的第44页,list数据结构中存在remove函数。但是,当我在解释器中尝试示例时,我不断收到错误。有谁知道为什么?下面是一个示例scala“删除”不起作用

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) 

回答

9

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

+2

你能更具体吗?在弃用filterNot已被命名为替代和一个测试对我产生了相同的结果。 – drexin 2013-02-19 06:38:06

+0

我的错误。我误解了'-'。 – 2013-02-19 15:11:48