I have a list of rows from a dataset that I need to iterate through.
The problem is that the processing in the iteration may delete one or more rows from the list.
Since the list is being modified, I can't use a foreach() loop.
But since it is possible some of the deletions may occur at elements BEFORE the one I'm processing, I also can't use a for() loop (i.e, if I'm processing element , and that results in the deletion of element and also other elements , I can't think of a way to adjust i to correctly point to the element following the one that I was processing).
How would you tackle this problem? My current thought it is to always process the first element in the list. If it gets deleted, process the new first element. If it doesn't get deleted, the move it to an "alreadyProcessed" list, and process the new first element.
Is there an easier way?