开发者

Get Current Index for Removal in String Collection

开发者 https://www.devze.com 2023-01-08 18:33 出处:网络
I have a String Collection that is populated with ID\'s like so --> 12345 23456 34567 and so on.What I need to do is at the user\'s request, and when certain parameters are met, go through that lis

I have a String Collection that is populated with ID's like so -->

12345
23456
34567

and so on. What I need to do is at the user's request, and when certain parameters are met, go through that list, starting at the top, and perform a method() using that ID. If successful I would remove it from the list and move on.

I, embarrassingly, have never worked with a collection before in this manner. Can someone point me in the right direction. Examples all seem to be of the Console.Writeline(""); vari开发者_高级运维ety.

My base, ignorant, attempt looks like this -->

 var driUps = Settings.Default.DRIUpdates.GetEnumerator();
        while (driUps.MoveNext())
        {
            var wasSuccessfull = PerformDRIUpdate(driUps.Current);
            if (wasSuccessfull)
            {
                driUps.Current.Remove(driUps.Current.IndexOf(driUps.Current));
            }
        }

The part I am most concerned with is the Remove(); Isn't there a better way to get the Current Index? Any and all Tips, Hints, Criticism, Pointers, etc....welcome. Thanks!


You are quite right to be concerned about the 'remove' during enumeration. How about somethign like this:

int idx = 0;
while (idx < strCol.Count)
{
    var wasSuccessful = PerformDRIUpdate(strCol[idx]);
    if (wasSuccessful)
        strCol.RemoveAt(idx);
    else
        ++idx;
}


As suggested by n8wrl, using RemoveAt solves the issue of trying to remove an item whilst enumerating the collection, but for large collections removing items from the front can cause performance issues as the underlying collection is re-built. Work your way from the end of the collection and remove items from that end:

//Loop backwards, as removing from the beginning
//causes underlying collection to be re-built
int index = (strCol.Count - 1);

while (index >= 0)
{
    if (PerformDRIUpdate(strCol[index]))
    { 
        strCol.RemoveAt(index);
    }

    --index;
}


Iterating an enumerator is best done with the foreach(), it does a GetEnumerator() and creates a similar block under the covers to what you're getting at, the syntax is:

foreach(ObjectType objectInstance in objectInstanceCollection)
{
    do something to object instance;
}

for you,

List<DRIUpdate> updatesToRemove = new List<DRIUpdate>();
foreach(DRIUpdate driUpdate in Settings.Default.DRIUpdates)
{
    if (PerformDRIUpdate(driUpdate))
    {
        updatesToRemove.Add(driUpdate);
    }
}

foreach(DRIUpdate driUpdate in updatesToRemove)
{
    Settings.Default.DRIUpdates.Remove(driUpdate);
}


If driUps is an IEnumerable<T>, try this:

driUps = driUps.Where(elem => !PerformDRIUpdate(elem));

Update:

From the example, it seems this is more appropriate:

Settings.Default.DRIUpdates = 
  Settings.Default.DRIUpdates.Where(elem => !PerformDRIUpdate(elem));

For a List<T>, it's simpler:

list.RemoveAll(PerformDRIUpdate);
0

精彩评论

暂无评论...
验证码 换一张
取 消