|
-
January 19th, 2012, 04:14 PM
#2
Re: Help with loop
It all depends on both list and how they are compared.
For example
(list1.Contains(wordFromList2)) == (list2.Contains(wordFromList1))
but
(!list1.Contains(wordFromList2)) != (!list2.Contains(wordFromList1))
Here is a quick example illustrating what I mean.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Create 2 list
string[] exampList1 = { "fruit", "friend", "dog", "cat", "people" };
string[] exampList2 = { "fruit", "cat", "rat", "human", "pie"};
Console.WriteLine("Example 1:");
Console.WriteLine("Strings from list 1 found in list 2\n");
// Loop goes through all strings found in list
foreach (string s in exampList1)
{
// Checks if string is found in the other list
if (exampList2.Contains(s))
{
// Writes out the string
Console.WriteLine(s);
}
}
Console.WriteLine("\n\nExample 2:");
Console.WriteLine("Strings from list 1 not found in list 2\n");
// Loop goes through all strings found in list
foreach (string s in exampList1)
{
// Checks if string is not found in other list
if (!exampList2.Contains(s))
{
// Writes out the string
Console.WriteLine(s);
}
}
Console.WriteLine("\n\nExample 3:");
Console.WriteLine("Strings from list 2 found in list 1\n");
// Loop goes through all strings found in list
foreach (string s in exampList2)
{
// Checks if string is not found in other list
if (exampList1.Contains(s))
{
// Writes out the string
Console.WriteLine(s);
}
}
Console.WriteLine("\n\nExample 4:");
Console.WriteLine("Strings from list 2 not found in list 1\n");
// Loop goes through all strings found in list
foreach (string s in exampList2)
{
// Checks if string is not found in other list
if (!exampList1.Contains(s))
{
// Writes out the string
Console.WriteLine(s);
}
}
// Wait for user input before closing application
Console.Write("\n\nPress any key to continue...");
Console.ReadKey();
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|