我必须从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
会在这里有用吗?
答案 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