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

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

Related

Add item in list in specific position

I'm trying to find if i can add an item in list in specific position.
Example
string[] tokens= new string[10];
tokens[5]="TestString";
when i'm trying this to list
List<string> mitems = new List<string>();
mitems.Insert(5, "TestString");
I'm getting error list inside a list index out of range.
Is there any relative to this for a list?
Use the Insert(index, item); method.
Have a look at MSDN Insert for more information.
But you will get an error when you're trying to insert an item at an index which is not existing.
You could init your list with 10 empty values like you did with your array but if you use Insert a new entry is created and not an old replaced like with a dictionary. That would mean you would have 11 entries after the first use of Insert
This example code
var items = new List<string>();
items.AddRange(Enumerable.Repeat(string.Empty, 10));
Console.WriteLine(items.Count);
items.Insert(5, "TestString");
Console.WriteLine(items.Count);
gives this output (for better understanding):
10
11
private static void Myfunc()
{
List<string> l = new List<string>();
string opt = "y";
while (opt == "y")
{
Console.WriteLine("Do you want to add in a specific position? (y/n)");
string pos = Console.ReadLine();
if (pos == "y")
{
Console.WriteLine("Which index you want to add?");
int index = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Add items in {0}", index);
l.Insert(index, Console.ReadLine());
}
else
{
Console.WriteLine("Enter to add in a list");
l.Add(Console.ReadLine());
Console.WriteLine("Do you wish to continue? (y/n)");
opt = Console.ReadLine();
}
Console.WriteLine("Do you want to print the list? (y/n)");
string print = Console.ReadLine();
if (print == "y")
{
foreach (var item in l)
{
Console.WriteLine(item);
}
}
}
I wrote this function for you.
Add this function to a console app for better understanding how list works for insert and append
EDIT 1:
I just saw your edit, another way of initializing a list with default values and then insert something in a certain position would be by initializing the list like this :-
List<string> l = Enumerable.Repeat("something blank", 10).ToList();
And then add to an index of your choice
Following adds default values of string at every index from 0-9
string[] tokens= new string[10];
But list is created on heap nothing instantiated. No default assigned values.
List<string> mitems = new List<string>();
If you try following it will fail as there are no values at 0-5
mitems.Insert(5, "TestString");
If you do following it will work
mitems.Insert(0, "TestString");
You can use List<T>.Insert(int, T) method to do that, example:
var tokens = new List<string>();
for (int i = 0; i < 10; i++)
tokens.Add(string.Empty);
tokens.Insert(5, "TestString");
See MSDN
Edit:
If you were just trying to replace the item in index of 5, than the [] will also do the trick as following example:
var tokens = new List<string>(10);
for (int i = 0; i < 10; i++)
tokens.Add(string.Empty);
tokens[5] = "TestString";

Add to array list from another array list

I have a program where I am trying to move items from one arraylist to another via a listbox but when I try to add it to the the second arraylist it does not add there.
ArrayList list1 = new ArrayList();
ArrayList list2 = new ArrayList();
list1.Add(new Class(var1, var2, var3, var4, var5, var6, var7));
foreach (object o in list1)
{
class m = (class)o;
selectionBox.Items.Add(m);
}
I initialised everything above and added everything to the class and then to the listbox. Note the vars I have got from an XML file.
bool req = true;
if (selectionBox.SelectedItem != null)
{
Count++;
errorLabel.Text = "";
for (int i = 0; i < selectionBox.Items.Count; i++)
{
if (selectionBox.GetSelected(i) == true)
{
class m = selectionBox.SelectedItem as class;
if (m.var2 == ((Modules)selectionBox.Items[i]).var2)
{
list2.Add(list1.IndexOf(i));
}
}
}
}
else
{
errorLabel.Text = "Error";
}
Here I am trying to add it to the second array list but it does not work the if statement however is correct I have tried this with print statements. So can someone tell me why the following line does not add to the list?
list2.Add(list1.IndexOf(i));
list2.Add(list1.IndexOf(i)); will give you the index (position) of each element. Not the element itself.
To add the element you would need to do something like this:
list2.Add(list1[i]);
Also, just as an aside, this will only copy the reference to each element, it will not create a new copy of each.

Add to array list from listbox selected item does not work properly

I have a program where I am trying to move items from one arrayList to another via a listbox and then print out the info in an XML, but the error I have is when I am adding it often certain times the values would repeat, when there are no repeats.
ArrayList list1 = new ArrayList();
ArrayList list2 = new ArrayList();
list1.Add(new RandomClass(var1, var2, var3, var4, var5, var6, var7));
foreach (object o in list1)
{
RandomClass m = (RandomClass)o;
selectionBox.Items.Add(m);
}
This is my initialization code.
bool req = true;
if (selectionBox.SelectedItem != null)
{
Count++;
errorLabel.Text = "";
for (int i = 0; i < selectionBox.Items.Count; i++)
{
if (selectionBox.GetSelected(i) == true)
{
RandomClass m = selectionBox.SelectedItem as RandomClass;
if (m.var2 == ((RandomClass)selectionBox.Items[i]).var2)
{
list2.Add(list1[i]);
}
}
}
}
else
{
errorLabel.Text = "Error";
}
Here is where I add to another array list. However as I said often the item would repeat and not be different, how can I resolve this problem?
Try clearing the second list each time you scan and add items from the first list.
list2.Clear();
for (int i = 0; i < selectionBox.Items.Count; i++)
....
I have fixed this problem using a list with my class, and there does not seem to be a problem.
List<RandomClass> list2 = new List<RandomClass>();
And then when adding I just simply put the following in the if statement
list2.Add(m);

C# List - Removing items while looping / iterating [duplicate]

This question already has answers here:
How to remove elements from a generic list while iterating over it?
(28 answers)
Closed 5 years ago.
Suppose that I have the following code snippet:
var data=new List<string>(){"One","Two","Three"};
for(int i=0 ; i<data.Count ; i++){
if(data[i]=="One"){
data.RemoveAt(i);
}
}
The following code throws exception.
My question is what is the best way to avoid this exception and to remove the element while looping?
If you need to remove elements then you must iterate backwards so you can remove elements from the end of the list:
var data=new List<string>(){"One","Two","Three"};
for(int i=data.Count - 1; i > -1; i--)
{
if(data[i]=="One")
{
data.RemoveAt(i);
}
}
However, there are more efficient ways to do this with LINQ (as indicated by the other answers).
You can use List<T>.RemoveAll to handle this:
data.RemoveAll(elem => elem == "One");
You can also use a forward moving loop like:
var data = new List<string>() { "One", "Two", "Three", "One", "One", "Four" };
for (int i = 0; i < data.Count; i++)
{
if (data[i] == "One")
{
data.RemoveAt(i--);
}
}
This line data.RemoveAt(i--); is stopping the effect of increment in the iteration variable at the end of the loop, in case of item being removed from the list.
It will remove the item from index at the current iteration value and then after removing the item, the iterator would be set to one less value than the current one. At the end of loop, the increment in loop body will move it to next valid index.
Here is a working dotfiddle
(Please note, that I personally use reverse loop for situation like these because IMO, they are easier to understand, this answer here is just for displaying another way of achieving it).
I happen to come across a simple solution for this using foreach and .ToArray()
var data=new List<string>(){"One","Two","Three"};
foreach ( var d in data.ToArray()){
if(d =="One"){
data.Remove(d);
}
}
You could try ChrisF's reverse iteration method to remove your item.
You could also simply:
List.Remove("One");
Or:
List.RemoveAll(i => i == "One"); // removes all instances
And be done with it. There's really no point in iterating over the collection to remove a single item.
Why don't you just simply decrement the iterator variable?
var data=new List<string>(){"One","Two","Three"};
for(int i=0 ; i<data.Count ; i++){
if(data[i]=="One"){
data.RemoveAt(i);
i--; // <<<<<<<<<<<
}
}
Here is a dirty trick and I wonder what the critique of it will be?
var data=new List<string>(){"One","Two","Three"};
foreach (string itm in (data.ToArray()))
{
if string.Compare(name, "one", true) == 0) data.Remove(name);
}
var data=new List<string>(){"One","Two","Three"};
for(int i=0; i<data.Count; ){
if(data[i]=="One") data.RemoveAt(i);
else ++i;
}
The general solution below makes a copy of the list and handles the negative index:
foreach (void item_loopVariable in MyList.ToList) {
item = item_loopVariable;
}
var data = new List<string>() { "One", "Two", "Three" };
data.RemoveAll(p=>p=="One");
You can use Stack class
Stack<string> myStack = new Stack<string>();
foreach (var item in Enumerable.Range(1,1001))
myStack.Push("Str " + item.ToString());
while (myStack.Any())
Console.WriteLine("Now {0} items in Stack, removed item is {1}",myStack.Count,myStack.Pop());
Console.ReadKey();
I had to remove more than one item from the list. so,I reinitialized the list count. Is there any other better option?
for (int i = dtList.Count - 1; dtList.Count > 0; )
{
DateTime tempDate = dtList[i].Item1.Date;
var selectDates = dtList.FindAll(x => x.Item1.Date == tempDate.Date);
selectDates.Sort((a, b) => a.Item1.CompareTo(b.Item1));
dtFilteredList.Add(Tuple.Create(selectDates[0].Item1, selectDates[0].Item2));
dtList.RemoveAll(x => x.Item1.Date == tempDate.Date);
i = dtList.Count - 1;
}
List<string> list = new List<string>();
list.Add("sasa");
list.Add("sames");
list.Add("samu");
list.Add("james");
for (int i = list.Count - 1; i >= 0; i--)
{
list.RemoveAt(i);
}

C# Comparing two sorted lists and outputting to a file

I'm trying to compare a list of strings compiled together against a master list and print them out to a text file. The problem I'm having is the printable list remains empty. How do I populate the third list? And, is this a proper use of List<>, if not, what should I use?
Edit: Sorry about that, prior to this method running, textInput and textCompare read from two files and are populated with strings 7 characters in length: one pulled from a text file, the other from an excel sheet. I then remove any nulls, and attempt to compare the two lists with listA.intersects(listB). MSDN mentioned it need to be enumerated through for the intersects to work, which is why I put it in a foreach.
void Compare()
{
List<string> matches = new List<string>();
textInput.Sort();
textCompare.Sort();
progressBar.Maximum = textInput.Count;
int increment = 0;
for (int i = textCompare.Count - 1; i >= 0; i--)
{
if (textCompare[i] == null)
{
textCompare.RemoveAt(i);
}
}
foreach (string item in textInput)
{
matches = textInput.Intersect(textCompare).ToList();
increment++;
progressBar.Value = increment;
}
//A break point placed on the foreach reveals matches is empty.
foreach (object match in matches)
{
streamWriter.WriteLine(match);
}
doneLabel.Text = "Done!";
}
From the description in your comment this would do it:
var textOutput = textCompare.Where(s => !string.IsNullOrEmpty(s))
.Intersect(textInput)
.OrderBy(s => s);
File.WriteAllLines("outputfile.txt", textOutput);
Note that you can remove the .Where() condition provided you don't have empty strings in your masterlist "textInput" (very likely there aren't). Also, if order doesn't matter remove the .OrderBy(), you end up with this then:
var textOutput = textCompare.Intersect(textInput);
File.WriteAllLines("outputfile.txt", textOutput);
Not sure why you have this in the loop.
foreach (string item in textInput)
{
matches = textInput.Intersect(textCompare).ToList();
increment++;
progressBar.Value = increment;
}
you just need
matches = textInput.Intersect(textCompare).ToList();
if you try something like
List<string> matches = new List<string>();
List<string> textInput = new List<string>(new[] {"a", "b", "c"});
textInput.Sort();
List<string> textCompare = new List<string>(new[] { "b", "c", "d" }); ;
textCompare.Sort();
int increment = 0;
for (int i = textCompare.Count - 1; i >= 0; i--)
{
if (textCompare[i] == null)
{
textCompare.RemoveAt(i);
}
}
matches = textInput.Intersect(textCompare).ToList();
matches should have { "b , "c" }. so your problem might be somewhere else.
Consider something like this:
are the 2 sort calls even necessary?
use LINQ extension methods to remove the blank/nulls
void Compare()
{
textCompare.RemoveAll(x => string.IsNullOrEmpty(x));
List<string> matches= textInput.Intersect(textCompare).ToList();
matches.ForEach(x=> streamWriter.WriteLine(x));
doneLabel.Text = "Done!";
}

Categories