Monday, January 9, 2012

How to substract 2 ArrayLists in C#?

Well the answer might be really easy for some of you but for newbies is not.
So considering that we have 2 arraylists.
 
ArrayList FirstArray = new ArrayList(new[] { "23", "25", "27" });
ArrayList SecondArray = new ArrayList(new[] { "25", "28", "29" });


and we want the difference.For that this is what you have to do. Call a method that I implemented.
 
ArrayList Difference = Substract2ArrayLists(FirstArray, SecondArray, true);
Method:
 
    /// 
    /// Substracts 2 array lists/a difference
    /// 
    /// 

First ArrayList
    /// 

Second ArrayList
    /// 

True if compare string, False for long values
    /// 
    static public ArrayList Substract2ArrayLists(ArrayList First, ArrayList Second, bool IsString)
    {
        if (IsString)
        {
            foreach (string secondValue in Second)
            {
                First.Remove(secondValue);
            }
        }
        else
        {
            foreach (long secondValue in Second)
            {
                First.Remove(secondValue);
            }
        }
        return First;
    }

No comments:

Post a Comment