2014-07-16 113 views
0

我必須從Array中刪除所有List元素。如何從數組中刪除所有列表元素?

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會在這裏有用嗎?

回答

8

您應該使用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) 
2

真正使用diff導致一個整潔和簡單的方法;一些其他更詳細的方式,

numbers filterNot { nums.contains(_) } 

for (n <- numbers if !nums.contains(n)) yield n 
相關問題