views:

373

answers:

10

Should you be allowed to delete an item from the collection you are currently iterating in a foreach loop? If so, what should be the correct behavior?

A: 

It really depends on the language. Some just hammer through an array and explode when you change that array. Some use arrays and don't explode. Some call iterators (which are wholly more robust) and carry on just fine.

Oli
+2  A: 

I can take quite a sophisticated Collection to support enumerators that track changes to the collection to keep position info correct. Even if it does some compromisation or assumptions need to be made. For that reason most libraries simply outlaw such a thing or mutter about unexpected behaviour in their docs.

Hence the safest approach is to loop. Collect references to things that need deleting and subsequently use the collected references to delete items from the original collection.

AnthonyWJones
A: 

You have to understand the concept of the foreach first, and actually it depends on the programming language. But as a general answer you should avoid changing your collections inside foreach

Ahmed Said
+1  A: 

Generally, modifying a collection in a foreach loop is a bad idea, because your intention is unknown to the program. Did you mean to loop through all items before the change, or do you want it to just go with the new configuration? What about the items that have already been looped through?

Instead, if you want to modify the collection, either make a predefined list of items to loop through, or use indexed looping.

Blixt
+1  A: 

Some collections such as hash tables and dictionaries have no notion of "position" and the order of iteration is generally not guaranteed. Therefore it would be quite difficult to allow deletion of items while iterating.

Jakob Christensen
A: 

Just use a standard for loop, iterate through the item collection backwards and you should have no problem deleting items as you go.

Nick
A: 

Personally I would use the RemoveAll of the collection in order to remove all the items that match my specific condition e.g.

List<string> names = new List<string>() { "james", "john", "steven", "alan", "mark" };
var countRemoved = names.RemoveAll(delegate(string name) { return name.Contains("j"); });

James.

James
You assume C#, and as we all know assume makes an 'ass' out of 'u' and 'me'...
thijs
Woops my mistake thought this was tagged as C#! Apologies :)
James
A: 

iterate in reverse direction and delete item one by one... That should proper solution.

Sandeep
A: 

No, you should not. The correct behaviour should be to signal that a potential concurrency problem has been encountered, however that is done in your language of choice (throw exception, return error code, raise() a signal).

If you modify a data structure while iterating over its elements, the iterator might no longer be valid, which means that you risk working on objects that are no longer part of the collection. If you want to filter elements based on some more complex notation, you could do something like this (in Java):

List<T> toFilter = ...;
List<T> shadow;
for ( T element : toFilter )
    if ( keep(element) )
         shadow.add(element);

/* If you'll work with toFilter in the same context as the filter */
toFilter = shadow;

/* Alternatively, if you want to modify toFilter in place, for instance if it's
 * been given as a method parameter
 */
toFilter.clear();
toFilter.addAll(shadow);
Christoffer
A: 

The best way to remove an item from a collection you are iterating over it to use the iterator explitly. For example.

List<String> myList = ArrayList<String>();
Iterator<String> myIt = myList.iterator();

while (myIt.hasNext()) {
    myIt.remove();
}
mR_fr0g