如何从数组中删除所有List元素?

时间:2014-07-16 14:01:56

标签: scala scala-collections

我必须从List删除所有Array个元素。

scala> var numbers=Array("321","3232","2401","7777","666","555")
numbers: Array[String] = Array(321, 3232, 2401, 7777, 666, 555)

scala> var nums=List("321","3232","2401")
nums: List[String] = List(321, 3232, 2401)

filter会在这里有用吗?

2 个答案:

答案 0 :(得分:10)

你应该使用numbers.diff(nums) - 就这么简单:

scala> var numbers = Array("321", "3232", "2401", "7777", "666", "555")
numbers: Array[String] = Array(321, 3232, 2401, 7777, 666, 555)

scala> var nums = List("321", "3232", "2401")
nums: List[String] = List(321, 3232, 2401)

scala> numbers diff nums
res0: Array[String] = Array(7777, 666, 555)

答案 1 :(得分:2)

真正使用diff会导致一种简洁明了的方法;其他一些更冗长的方式,

numbers filterNot { nums.contains(_) }

for ( n <- numbers if !nums.contains(n) ) yield n