locked
how to check if an ArrayList of objects contains my object? RRS feed

  • Question

  • Hi,

    I have a List of objects of the class : that is defined below :

     public class NewCompactSimilar
        {
            public List<int> offsets;
            public List<String> words;
            public int pageID;
    
            public NewCompactSimilar()
            {
                offsets = new List<int>();
                words = new List<string>();          
            }
        }

     I need to check if my ArrayList contains an object that is identical to my object.

    So, how could I doing so?

    Thanks so much.

    Best Regards,

    Aya.


    Aya Zoghby

    Sunday, December 16, 2012 9:58 PM

Answers

    • Override Equals method in class NewCompactSimilar. This makes it easier to compare object of this type
        public class NewCompactSimilar
        {
            public List<int> offsets;
            public List<String> words;
            public int pageID;
    
            public NewCompactSimilar()
            {
                offsets = new List<int>();
                words = new List<string>();
            }
    
            public override bool Equals(object obj)
            {
                NewCompactSimilar ncs = obj as NewCompactSimilar;
    
                if (ncs == null) return false;
    
                else
                {
                    if (this.offsets.Count != ncs.offsets.Count) return false;
                    else
                    {
                        foreach (int offset in this.offsets)
                        {
                            if (ncs.offsets.Contains(offset) == false) return false;
                        }
                    }
    
                    if (this.words.Count != ncs.words.Count) return false;
                    else
                    {
                        foreach (string word in this.words)
                        {
                            if (ncs.words.Contains(word) == false) return false;
                        }
                    }
    
                    if (this.pageID != ncs.pageID) return false;
    
                    return true;
                }
            }
        }
    • Now assuming the arraylist has objects and you can find it using following code
                ArrayList list = new ArrayList();
                //......Populate list with NewCompactSimilar Objects
    
                NewCompactSimilar n = new NewCompactSimilar();
                //......Set the properties of n
    
                bool foundFlag = false;
                foreach (NewCompactSimilar ncs in list)
                {
                    if (ncs.Equals(n))
                    {
                        //Object already exists
                        foundFlag = true;
                        break;
                    }                
                }
    
                if (foundFlag == false)
                {
                    //Object not exists
                }

    I used the following pattern to implement this

    http://msdn.microsoft.com/en-us/library/bhkz42b3.aspx


    A.m.a.L Hashim
    Microsoft Most Valuable Professional
    My Blog - Dot Net Goodies


    • Edited by A.m.a.L Hashim Monday, December 17, 2012 4:53 AM link
    • Proposed as answer by Shyam Kr Monday, December 17, 2012 12:49 PM
    • Marked as answer by ZoghbyAya Monday, December 17, 2012 8:09 PM
    Monday, December 17, 2012 4:50 AM

All replies

    • Override Equals method in class NewCompactSimilar. This makes it easier to compare object of this type
        public class NewCompactSimilar
        {
            public List<int> offsets;
            public List<String> words;
            public int pageID;
    
            public NewCompactSimilar()
            {
                offsets = new List<int>();
                words = new List<string>();
            }
    
            public override bool Equals(object obj)
            {
                NewCompactSimilar ncs = obj as NewCompactSimilar;
    
                if (ncs == null) return false;
    
                else
                {
                    if (this.offsets.Count != ncs.offsets.Count) return false;
                    else
                    {
                        foreach (int offset in this.offsets)
                        {
                            if (ncs.offsets.Contains(offset) == false) return false;
                        }
                    }
    
                    if (this.words.Count != ncs.words.Count) return false;
                    else
                    {
                        foreach (string word in this.words)
                        {
                            if (ncs.words.Contains(word) == false) return false;
                        }
                    }
    
                    if (this.pageID != ncs.pageID) return false;
    
                    return true;
                }
            }
        }
    • Now assuming the arraylist has objects and you can find it using following code
                ArrayList list = new ArrayList();
                //......Populate list with NewCompactSimilar Objects
    
                NewCompactSimilar n = new NewCompactSimilar();
                //......Set the properties of n
    
                bool foundFlag = false;
                foreach (NewCompactSimilar ncs in list)
                {
                    if (ncs.Equals(n))
                    {
                        //Object already exists
                        foundFlag = true;
                        break;
                    }                
                }
    
                if (foundFlag == false)
                {
                    //Object not exists
                }

    I used the following pattern to implement this

    http://msdn.microsoft.com/en-us/library/bhkz42b3.aspx


    A.m.a.L Hashim
    Microsoft Most Valuable Professional
    My Blog - Dot Net Goodies


    • Edited by A.m.a.L Hashim Monday, December 17, 2012 4:53 AM link
    • Proposed as answer by Shyam Kr Monday, December 17, 2012 12:49 PM
    • Marked as answer by ZoghbyAya Monday, December 17, 2012 8:09 PM
    Monday, December 17, 2012 4:50 AM
  • Hi ZoghbyAya

    Just use IndexOf method:

    public class NewCompactSimilar
        {
            public List<int> offsets;
            public List<String> words;
            public int pageID;
    
            public NewCompactSimilar()
            {
                offsets = new List<int>();
                words = new List<string>();          
            }
        }
    
    NewCompactSimilar n = new NewCompactSimilar();
    if(m.offsets.IndexOf(Your New Int here)>=0)
    {
       //……
    }
    if(m.words.IndexOf(Your New String here)>=0)
    {
       //……
    }

    我的博客园
    慈善点击,点击此处
    和谐拯救危机,全集下载,净化人心

    Monday, December 17, 2012 8:22 AM
  • Hi ZoghbyAya

    Just use IndexOf method:

    public class NewCompactSimilar
        {
            public List<int> offsets;
            public List<String> words;
            public int pageID;
    
            public NewCompactSimilar()
            {
                offsets = new List<int>();
                words = new List<string>();          
            }
        }
    
    NewCompactSimilar n = new NewCompactSimilar();
    if(m.offsets.IndexOf(Your New Int here)>=0)
    {
       //……
    }
    if(m.words.IndexOf(Your New String here)>=0)
    {
       //……
    }

    我的博客园
    慈善点击,点击此处
    和谐拯救危机,全集下载,净化人心

    Thank you so much for your reply.

    However, I need to check the whole contents of the object of type 'NewCompactSimilar' . Note that the properties 'offsets' and 'words' are Lists.

    I used the following statement to do so , but it failed:

    if (((DocumentMetaData)myDocumnetsMetaData[pageID]).morphological_occuerences.Contains(compactSimilar))
    {
    ....
    }

    the 'morphological_occuerences' is the ArrayList that contains 'NewCompactSimilar' object.

    Thanks,

    Best Regards,

    Aya.


    Aya Zoghby

    Monday, December 17, 2012 12:43 PM