Exception during iteration on collection and remove items from that collection [duplicate]

放肆的年华 提交于 2019-11-28 01:08:44

问题


This question already has an answer here:

  • What is the best way to modify a list in a 'foreach' loop? 11 answers

I remove item from ArrayList in foreach loop and get follwing exception.

Collection was modified; enumeration operation may not execute.

How can I remove items in foreach,

EDIT: There might be one item to remove or two or all.

Following is my code:

/*
 * Need to remove all items from 'attachementsFielPath' which does not exist in names array.
 */

try
{
    string attachmentFileNames = txtAttachment.Text.Trim(); // Textbox having file names.
    string[] names = attachmentFileNames.Split(new char[] { ';' });

    int index = 0;

    // attachmentsFilePath is ArrayList holding full path of fiels user selected at any time.
    foreach (var fullFilePath in attachmentsFilePath)
    {
        bool isNeedToRemove = true;

        // Extract filename from full path.
        string fileName = fullFilePath.ToString().Substring(fullFilePath.ToString().LastIndexOf('\\') + 1);

        for (int i = 0; i < names.Length; i++)
        {
        // If filename found in array then no need to check remaining items.
        if (fileName.Equals(names[i].Trim()))
        {
            isNeedToRemove = false;
            break;
        }
        }

        // If file not found in names array, remove it.
        if (isNeedToRemove)
        {
        attachmentsFilePath.RemoveAt(index);
        isNeedToRemove = true;
        }

        index++;
    }
}
catch (Exception ex)
{
    throw ex;
}

EDIT: Can you also advice on code. Do I need to break it into small methods and exception handling etc.

Invalid argument exception On creating generic list from ArrayList

foreach (var fullFilePath in new List<string>(attachmentsFilePath))

{

When I use List<ArrayList> the exception is Argument '1': cannot convert from 'System.Collections.ArrayList' to 'int'

attachmentsFilePath is declared like this

ArrayList attachmentsFilePath = new ArrayList();

But when I declared it like this, problem solved

List<ArrayList> attachmentsFilePath = new List<ArrayList>();

回答1:


You can iterate over a copy of the collection:

foreach(var fullFilePath in new ArrayList(attachmentsFilePath))
{
    // do stuff
}



回答2:


Another way of doing it, start from the end and delete the ones you want:

List<int> numbers = new int[] { 1, 2, 3, 4, 5, 6 }.ToList();
for (int i = numbers.Count - 1; i >= 0; i--)
{
    numbers.RemoveAt(i);
}



回答3:


You can't remove an item from a collection while iterating over it.

You can find the index of the item that needs to be removed and remove it after iteration has finished.

int indexToRemove = 0;

// Iteration start

if (fileName.Equals(names[i].Trim()))
{
    indexToRemove = i;
    break;
}

// End of iteration

attachmentsFilePath.RemoveAt(indexToRemove);

If, however, you need to remove more than one item, iterate over a copy of the list:

foreach(string fullFilePath in new List<string>(attachmentsFilePath))
{
    // check and remove from _original_ list
}



回答4:


    List<string> names = new List<string>() { "Jon", "Eric", "Me", "AnotherOne" };
    List<string> list = new List<string>() { "Person1", "Paerson2","Eric"};

    list.RemoveAll(x => !names.Any(y => y == x));
    list.ForEach(Console.WriteLine);



回答5:


while enumerating (or using foreach) you cannot modify that collection. If you really want to remove items, then you can mark them and later remove them from list using its Remove method




回答6:


do the following:

foreach (var fullFilePath in new List(attachmentsFilePath))
{

this way you create a copy of the original list to iterate through




回答7:


You could loop over the collection to see which items need to be delete and store those indexes in a separate collection. Finally you would need to loop over the indexes to be deleted in reverse order and remove each from the original collection.

list<int> itemsToDelete

for(int i = 0; i < items.Count; i++)
{
    if(shouldBeDeleted(items[i]))
    {
        itemsToDelete.Add(i);
    }
}

foreach(int index in itemsToDelete.Reverse())
{
    items.RemoveAt(i);
}


来源:https://stackoverflow.com/questions/2545027/exception-during-iteration-on-collection-and-remove-items-from-that-collection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!