Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have two array lists of different sizes. suppose list1 have 4 values and list 2 have two values. My question is "how to check whether list 2 values are present in list1 or not ?
Posted
Comments
Jameel VM 21-Jun-13 2:45am    
can you post the code of the two array lists?

Check this. I think you will get your solution.

code:

ArrayList numList1 = new ArrayList(new int[] { 1, 2, 3, 4});
            ArrayList numList2 = new ArrayList(new int[] { 1, 5, 3});


            ArrayList numListDiff3 = new ArrayList();
            foreach (int listElement in numList1)
            {
                if (!numList2.Contains(listElement))
                {
                    numListDiff3.Add(listElement);
                }
            }


            foreach (int listElement in numList2)
            {
                if (!numList1.Contains(listElement))
                {
                    numListDiff3.Add(listElement);
                }
            }

Source: http://forums.asp.net/post/3573258.aspx[^]

[Edit]Reference to original answer added[/Edit]
 
Share this answer
 
v2
Something like this:
C#
ArrayList list1 = new ArrayList(new int[] {1,2,3,4,5});
ArrayList list2 = new ArrayList(new int[] {1,2,8});
for (int i=0; i<list1.count && i<list2.Count; i++) {
    if (!Object.Equals(list1[i], list2[i]))
        Console.WriteLine("Different value at index {0}.", i);
}
 
Share this answer
 
v2
Hi,
I will propose to use Linq to do your operation.
You can find a discussion on this point:
http://stackoverflow.com/questions/9524681/linq-compare-two-lists[^]

Best regards.
 
Share this answer
 
ArrayList list1 = new ArrayList(new int[] {1,2,3,4,5});

ArrayList list2 = new ArrayList(new int[] {1,2,8});

for (int i=0; i<list1.Count && i<list2.Count; i++)

{

if (!Object.Equals(list1[i], list2[i]))

Console.WriteLine("Different value at index {0}.", i);

}

if (list1.Count > list2.Count)

Console.WriteLine("list1 has more elements than list2.");

if (list2.Count > list1.Count)

Console.WriteLine("list2 has more elements than list1.");

Console.ReadLine();

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900