473,499 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

delete element of collection being iterated

Hello.

Sometimes I encounter the following problem:

foreach( element of some collection)
{
delete this element;
}

of course, the runtime would not allow to modify
the collection whilst executing loop through
its elements.

I often happen to write solutoin to this problem
by myself, but i am curious if there is some
design pattern how to handle such a situation
(the need to delete element of collection being ierated
through)

Thaks for answer
Michal Januszczyk
Nov 22 '05 #1
3 1623
One trick is to use "for(){}" backward, insteadof "foreach(){}":

for (int i=myCollection.Count-1; i>=0; i--)
{
myCollection.RemoveAt(i)
//Or your own Delete(i)/Remove(i) method, because if you remove item
//from a collection backward, remaining item's index will not change
}

"Michal Januszczyk" <an*******@discussions.microsoft.com> wrote in message
news:03****************************@phx.gbl...
Hello.

Sometimes I encounter the following problem:

foreach( element of some collection)
{
delete this element;
}

of course, the runtime would not allow to modify
the collection whilst executing loop through
its elements.

I often happen to write solutoin to this problem
by myself, but i am curious if there is some
design pattern how to handle such a situation
(the need to delete element of collection being ierated
through)

Thaks for answer
Michal Januszczyk

Nov 22 '05 #2
Hi,

I also faced this problem and incorporated the following
solution for the same.

// Create a Hashtable and clone the existing HashTable
(accessible through the InnerHashtable property of the
types derived from the DictionaryBase class).

System.Collections.Hashtable LocalHashTable =
(System.Collections.Hashtable)this.InnerHashtable. Clone();

// Create an IEnumerator type instance, so as to traverse
the collection.

System.Collections.IEnumerator KeysEnumerator =
LocalHashTable.GetEnumerator();

// Finally traverse the collection
while (KeysEnumerator.MoveNext())
{
// Now you can manipulate the collection.
this.Dictionary.Remove(OldCellKey);
}

Regards,
Puneet Taneja

-----Original Message-----
One trick is to use "for(){}" backward, insteadof "foreach(){}":
for (int i=myCollection.Count-1; i>=0; i--)
{
myCollection.RemoveAt(i)
//Or your own Delete(i)/Remove(i) method, because if you remove item //from a collection backward, remaining item's index will not change}

"Michal Januszczyk" <an*******@discussions.microsoft.com> wrote in messagenews:03****************************@phx.gbl...
Hello.

Sometimes I encounter the following problem:

foreach( element of some collection)
{
delete this element;
}

of course, the runtime would not allow to modify
the collection whilst executing loop through
its elements.

I often happen to write solutoin to this problem
by myself, but i am curious if there is some
design pattern how to handle such a situation
(the need to delete element of collection being ierated
through)

Thaks for answer
Michal Januszczyk

.

Nov 22 '05 #3
If you create the iterator against a clone of the collection, you can delete from the original collection while continuing to iterate the clone

"
Private ie as IEnumerator = MyList.Clone.GetEnumerator()
"

From http://www.developmentnow.com/g/34_2...g-iterated.htm

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com
Sep 8 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
331
by: Michal Januszczyk | last post by:
Hello. Sometimes I encounter the following problem: foreach( element of some collection) { delete this element; } of course, the runtime would not allow to modify
3
7953
by: Grandma Wilkerson | last post by:
Hi, The documentation states that enumeration through a collection is inherently NOT thread-safe, since a thread which added/removed an item from said collection could screw up the thread that...
7
1878
by: Marc Bishop | last post by:
Hi can anyone help? I'm making a shopping cart and am stuck on removing an item from my array? The array is made : cArray(ITEM_NAME,cItem) = ProductName
3
7573
by: Jeff L. | last post by:
I have an interesting problem and I'm not coming up with any answers in my searches, so hopefully someone can give me a hand with this. I have a feeling it's easy, but I usually get my nose stuck...
13
14696
by: Joseph Garvin | last post by:
When I first came to Python I did a lot of C style loops like this: for i in range(len(myarray)): print myarray Obviously the more pythonic way is: for i in my array: print i
10
12162
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
4
2755
by: Kyote | last post by:
I'm trying to persist a list of filenames. I've made a custom collection and a FileName class: 'Class to hold file name information Public Class FileNames Public fullName As String Public...
11
2919
by: shiniskumar | last post by:
Ive got a collection with values Object= hashmap(k,v) Object= hashmap(k,v) Object= hashmap(k,v) Object= hashmap(k,v) Object= hashmap(k,v) i terated this collection and deleted object. then...
1
1717
by: shiniskumar | last post by:
how to delete the last element of a collection without iterating it?
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7014
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7395
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4921
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.