开发者

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

开发者 https://www.devze.com 2022-12-24 22:26 出处:网络
This question already has answers here: What is the best way to modify a list in a 'foreach' loop?
This question already has answers here: What is the best way to modify a list in a 'foreach' loop? (11 answers) Closed 9 years ago.

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))

{

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

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>();


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);
}


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
}


You can iterate over a copy of the collection:

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


    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);


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


do the following:

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

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


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号