I have to delete some rows from a data table. I've heard that it is not ok to change a collection while iterating through it. So instead of a for loop in which I check if a row meets the demands for deletion and then mark it as deleted, I should first iterate through the data table and add all of the rows in a list, then iterate through the list and mark the rows for deletions. What are the reasons for this, and what alternatives do I have (instead of using the rows list I mean)?.
It is not generally permissible for one thread to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under these circumstances.
Use Iterator#remove . This is the only safe way to modify a collection during iteration. For more information, see The Collection Interface tutorial. If you also need the ability to add elements while iterating, use a ListIterator .
Collections support foreach statement using Enumarator. Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection.
Iterating Backwards through the List sounds like a better approach, because if you remove an element and other elements "fall into the gap", that does not matter because you have already looked at those. Also, you do not have to worry about your counter variable becoming larger than the .Count.
List<int> test = new List<int>(); test.Add(1); test.Add(2); test.Add(3); test.Add(4); test.Add(5); test.Add(6); test.Add(7); test.Add(8); for (int i = test.Count-1; i > -1; i--) { if(someCondition){ test.RemoveAt(i); } }
Taking @bruno code, I'd do it backwards.
Because when you move backwards, the missing array indices do not interfere with the order of your loop.
var l = new List<int>(new int[] { 0, 1, 2, 3, 4, 5, 6 }); for (int i = l.Count - 1; i >= 0; i--) if (l[i] % 2 == 0) l.RemoveAt(i); foreach (var i in l) { Console.WriteLine(i); }
But seriuosly, these days, I'd use LINQ:
var l = new List<int>(new int[] { 0, 1, 2, 3, 4, 5, 6 }); l.RemoveAll(n => n % 2 == 0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With