Compare two string ArrayLists
I have two array lists
dim Colors1 = New ArrayList
Colors1.Add("Blue")
Colors1.Add("Red")
Col开发者_如何学编程ors1.Add("Yellow")
Colors1.Add("Green")
Colors1.Add("Purple")
dim Colors2 = New ArrayList
Colors2.Add("Blue")
Colors2.Add("Green")
Colors2.Add("Yellow")
I would like to find out which colors are missing from Colors2 that are found in Colors1
Look at using Except method. "This method returns those elements in first that do not appear in second. It does not also return those elements in second that do not appear in first."
So you can just put colors 2 as the first argument and colors1 as the second.
EDIT: I meant you can put colors 1 first and colors 2 as the second.
EDIT2: (per Sean)
var missingFrom2 = colors1.Except(colors2);
Just for completeness, I'll add the old-fashioned way.
List<string> result = new List<string>();
foreach (string s in Colors1)
if (Colors2.Contains(s) == false)
result.add(s);
// now result has the missing colors
精彩评论