473,408 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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 1618
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
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
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
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
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
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
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
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
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
by: shiniskumar | last post by:
how to delete the last element of a collection without iterating it?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.