This is probably something really simple but I haven't been able to find the answer. (one of those "too general of keywords makes searching neraly impossible" problems ;) )
What I have is a list of indexes that I need to remove from a list. My problem is that once I start removing them, it changes the objects that the other indexes point at. For example:
List<Object> objects =new List<Objects>();
List<int> removalIndexes = new List<int>(); |
| |
| for(int i=0;i<objects.Count;i++) |
| { |
| if(objects[i].MeetsRemovalConditions) |
| { |
| removalIndexes.Add(i); |
| } |
| } |
| |
| foreach(int removeIndex in removalIndexes) |
| { |
| objects.RemoveAt(removeIndex); |
| } |
Note that this is highly, highly simplified - the first loop is huge and other things are done to the objects inbetween the setting and actual removing so I can't remove the objects in the first loop like I normally would.
Anyway, as you can imagine in the second loop, as soon as it starts iterating the indexes change. I'm guessing there's another, better way of doing this; possibly with a lambda expression? I tried searching for that but couldn't find a way to check based on its index in the list.
I may just have to add a flag to the object itself to check if it needs to be removed, but I'd still like to know if there's a way to do this the way I originally intended, because I like cool C# tricks like this. ;D
"Software is never finished, it is in varying states of 'less broken'" because "If it ain't broke, it doesn't have enough features yet"
In Playtest:
Avatar Land |
The MANLY Game for MANLY Men The signature that was too big for the 512 char limit