简体   繁体   中英

Remove duplicates from arraylist of objects

I'm trying to remove duplicates of one arraylist filled with the objects "DataPerLabel".

DataPerLabel contains the following methods: getLabelname(), getLabelAdress() and getDataType().

Some background information:

The values labelname, labelAdress and dataType can be set as 1 object in an arraylist named allData by a submit button. When submit is pressed for the 2nd time i want to delete that object of the arraylist.

Picture: 例

Some of the code i've tried:

if (submitButtonClicked == true) {
  if (MessageBox.Show("This is item is already set. Do you want to delete?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {
    foreach(DataPerLabel item in allData) {
        if (item.getDataType().Equals(dataType) && item.getLabelAdress().Equals(adress) && item.getLabelName().Equals(label)) {
          allData.Remove(item);
        } else {

        }
    }
  } else {
    //no
  }

}

With this code i'm getting the following error: An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll.

Additional error information: The collection has been changed.

You are trying to iterate in allData list and you are also trying to modify the same list. You can keep removing datas in an other list(eg: removeItems) and then you can remove items. I mean :

   if (submitButtonClicked == true) {
      if (MessageBox.Show("This is item is already set. Do you want to delete?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {
List<DataPerLabel> removeItems = new List<DataPerLabel>();
        foreach(DataPerLabel item in allData) {
            if (item.getDataType().Equals(dataType) && item.getLabelAdress().Equals(adress) && item.getLabelName().Equals(label)) {
              removeItems.Add(item);
            } else {

            }
        }
      } else {
        //no
      }

    foreach(DataPerLabel removeItem in removeItems ){
        allData.Remove(removeItem);
      }

}
        //Here is just an example of list of array 
       List<object> obj = new List<object>();
        obj.Add(5);
        obj.Add(4);
        obj.Add("we");
        obj.Add(5);
        List<object> objTodelete = new List<object>();

        foreach (var item in obj)
        {
            int count = obj.Count(a => a.ToString() == item.ToString());
            //for finding the dublicate occurence
            if (count > 1)
            {
                //for removing that dublicated occurence.
                object Dublicate = obj.Find(m => m.ToString() == item.ToString());
                if (!objTodelete.Contains(Dublicate))
                {
                    objTodelete.Add(Dublicate); 
                }
            }
        }
        foreach (var itemDublicate in objTodelete)
        {
            obj.Remove(itemDublicate);
        }

As OnurBulbul said you get an exception due trying to modify the list you are iterating through. There are a lot of different solution could be. My suggestion is to use LINQ:

var sortedList = from item in allData
                 where !(item.getDataType().Equals(dataType) && item.getLabelAdress().Equals(adress) && item.getLabelName().Equals(label))
                 select item

It will create a new arrayList for you with all sorted out data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM