Red Squirrel
No Lifer
I tend to find various ways of doing this but thought I'd ask, what is the best or better way of doing this?
Lets say I have an array of items I want to loop through and delete items. So Either a Vector, or a C# List... I'm speaking generally here not caring about the language.
When I'm in the loop if I delete an item then it's size will change so the loop may not complete as expected.
What I normally do is loop through, adding all valid items to another array, then clear first and copy temp one over.
Now, is there a better way of doing it?
In this particular case I'm dealing with C# List object. This is my code:
Lets say I have an array of items I want to loop through and delete items. So Either a Vector, or a C# List... I'm speaking generally here not caring about the language.
When I'm in the loop if I delete an item then it's size will change so the loop may not complete as expected.
What I normally do is loop through, adding all valid items to another array, then clear first and copy temp one over.
Now, is there a better way of doing it?
In this particular case I'm dealing with C# List object. This is my code:
Code:
//this is used to check if anyone went offline or something, so we remove them from array
public bool ValidateRegisteredMobs()
{
List<Mobile> templist = new List<Mobile>();
for(int i=0;i<RegisteredMobs.Count;i++)
{
Mobile mob = RegisteredMobs[i] as Mobile;
if(mob.NetState==null || mob.Deleted)continue;
templist.Add(mob);
}
RegisteredMobs.Clear();
RegisteredMobs = templist;
templist.Clear();
return templist.Count>=MinMembers;
}