Compare ArrayList

  • Thread starter Thread starter bengamin
  • Start date Start date
B

bengamin

Hi,

I declare two ArrayList variables.How can I compare if the two ArrayList are
Value Equal.

Thanks!

Ben
 
If you want to compare if the two arraylist variables refer to the same
arrayList object then you can use the ArrayList.Equals method, which
compares the object references.

But i guess you want to comapre the contents of the array list to check
if the contents are equak or not. You will have to write your own method
to do that, something along the lines of

priavte bool CompareArrayLists(ArrayList arr1,ArrayList arr2)
{
//Check if the two arraysLists have the same length
if(arr1.Count != arr2.Count)
return false;

//Iterate through each element and compare if it is equal to the
corresponding element in the other

for(int i=0;i<arr1.Count;i++)
{
if(!arr1.Equals(arr2))
return false;
}

return true;
}

Of course the objects contained in the array lists should be comparable.
If they are custom objects then you can either override the Equals
vritual method, or use the ICompare interface.

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
 
Back
Top