473,473 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

remove ArrayList[n] inside foreach() loop?

Hi all,

I wanted to go through each entry(?) of ArrayList and remove some particular
entry. So I tried following but it throws exception at runtime:

foreach (myEntry entry in myArrayList)
{
// do something...

if (entry.fieldA == 0)
{ // remove entry
myArrayList.Remove(entry); // exception below
} // remove entry
}

/* exception:
System.InvalidOperationException: Collection was modified; enumeration
operation may not execute.
at System.Collections.ArrayList.ArrayListEnumeratorSi mple.MoveNext()
*/

So I had to create an index array to remove and remove indexed entries
outside of foreach() loop. Is this a natural way to do or is there a better
way?

Regards

Bob
Aug 27 '07 #1
10 18041
Hi,

"bbg" <bb*@discussions.microsoft.comwrote in message
news:0F**********************************@microsof t.com...
Hi all,

I wanted to go through each entry(?) of ArrayList and remove some
particular
entry. So I tried following but it throws exception at runtime:

foreach (myEntry entry in myArrayList)
{
// do something...

if (entry.fieldA == 0)
{ // remove entry
myArrayList.Remove(entry); // exception below
} // remove entry
}

/* exception:
System.InvalidOperationException: Collection was modified; enumeration
operation may not execute.
at System.Collections.ArrayList.ArrayListEnumeratorSi mple.MoveNext()
*/

So I had to create an index array to remove and remove indexed entries
outside of foreach() loop. Is this a natural way to do or is there a
better
way?
You cannot modify the collection you are iterating in. You have to keep
track of those elements that you want removed and remove them after the
colection is iterate. A way of doing it is
ArrayList toremove = new ...
foreach (myEntry entry in myArrayList)
{
// do something...

if (entry.fieldA == 0)
toremove.Add( entry)
}
foreach(myEntry entry in toremove )
myArrayList.Remove( entry);

Aug 27 '07 #2
Or, if you want to prevent having to make two iterations, you could just
iterate backwards through the list (so you can consistently traverse the
items in the list as you remove them) and remove the items as needed:

for (int index = myArrayList.Count - 1; index >= 0; index--)
{
// Get the item.
myEntry entry = (myEntry) myArrayList[index];

// Check to remove.
if (entry.fieldA == 0)
{
// Remove.
myArrayList.RemoveAt(index);
}
}
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.comwrote in
message news:%2****************@TK2MSFTNGP06.phx.gbl...
Hi,

"bbg" <bb*@discussions.microsoft.comwrote in message
news:0F**********************************@microsof t.com...
>Hi all,

I wanted to go through each entry(?) of ArrayList and remove some
particular
entry. So I tried following but it throws exception at runtime:

foreach (myEntry entry in myArrayList)
{
// do something...

if (entry.fieldA == 0)
{ // remove entry
myArrayList.Remove(entry); // exception below
} // remove entry
}

/* exception:
System.InvalidOperationException: Collection was modified; enumeration
operation may not execute.
at System.Collections.ArrayList.ArrayListEnumeratorSi mple.MoveNext()
*/

So I had to create an index array to remove and remove indexed entries
outside of foreach() loop. Is this a natural way to do or is there a
better
way?

You cannot modify the collection you are iterating in. You have to keep
track of those elements that you want removed and remove them after the
colection is iterate. A way of doing it is
ArrayList toremove = new ...
foreach (myEntry entry in myArrayList)
{
// do something...

if (entry.fieldA == 0)
toremove.Add( entry)
}
foreach(myEntry entry in toremove )
myArrayList.Remove( entry);

Aug 27 '07 #3
bob
On Mon, 27 Aug 2007 13:44:00 -0700, bbg
<bb*@discussions.microsoft.comwrote:
Hi,
Same theme as Ignacio,
But I would be tempted to save the wanted members to a new list
eliminating the remove step.
Depends I guess on how expensive it is to make a 'myEntry' and the
ratio of good to bad.
Bob C.
>Hi all,

I wanted to go through each entry(?) of ArrayList and remove some particular
entry. So I tried following but it throws exception at runtime:

foreach (myEntry entry in myArrayList)
{
// do something...

if (entry.fieldA == 0)
{ // remove entry
myArrayList.Remove(entry); // exception below
} // remove entry
}

/* exception:
System.InvalidOperationException: Collection was modified; enumeration
operation may not execute.
at System.Collections.ArrayList.ArrayListEnumeratorSi mple.MoveNext()
*/

So I had to create an index array to remove and remove indexed entries
outside of foreach() loop. Is this a natural way to do or is there a better
way?

Regards

Bob
Aug 27 '07 #4

"bob" <st**************@cutthis.adriley.co.nzwrote in message
news:hn********************************@4ax.com...
On Mon, 27 Aug 2007 13:44:00 -0700, bbg
<bb*@discussions.microsoft.comwrote:
Hi,
Same theme as Ignacio,
But I would be tempted to save the wanted members to a new list
eliminating the remove step.
Depends I guess on how expensive it is to make a 'myEntry' and the
ratio of good to bad.
Bob C.
That's what System.Collections.Generic.List<T>.RemoveAll does.

Most any use of ArrayList can be replaced by List<objectif not something
more typesafe.
Aug 28 '07 #5
Nope, there's an easier way to do this without the hocus pocus. Here is the
proof of concepts:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
System.Collections.ArrayList arr = new
System.Collections.ArrayList();
arr.Add("1");
arr.Add("2");
arr.Add("3");

/*This throws an exception
foreach (string s in arr)
{
arr.Remove(s);
}
*/

//where as this works like a charm
Console.WriteLine(arr.Count);
foreach (string s in new System.Collections.ArrayList(arr))
{
arr.Remove(s);
}
Console.WriteLine(arr.Count);
Console.ReadKey();
}
}
}
--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET - MS Press
Professional VSTO 2005 - Wrox/Wiley
OWC Black Book www.lulu.com/owc

"bbg" <bb*@discussions.microsoft.comwrote in message
news:0F**********************************@microsof t.com...
Hi all,

I wanted to go through each entry(?) of ArrayList and remove some
particular
entry. So I tried following but it throws exception at runtime:

foreach (myEntry entry in myArrayList)
{
// do something...

if (entry.fieldA == 0)
{ // remove entry
myArrayList.Remove(entry); // exception below
} // remove entry
}

/* exception:
System.InvalidOperationException: Collection was modified; enumeration
operation may not execute.
at System.Collections.ArrayList.ArrayListEnumeratorSi mple.MoveNext()
*/

So I had to create an index array to remove and remove indexed entries
outside of foreach() loop. Is this a natural way to do or is there a
better
way?

Regards

Bob

Aug 29 '07 #6

"Alvin Bruney [MVP]" <some guy without an email addresswrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Nope, there's an easier way to do this without the hocus pocus. Here is
the proof of concepts:
Sure, but it's the most inefficient solution posted yet.
>
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
System.Collections.ArrayList arr = new
System.Collections.ArrayList();
arr.Add("1");
arr.Add("2");
arr.Add("3");

/*This throws an exception
foreach (string s in arr)
{
arr.Remove(s);
}
*/

//where as this works like a charm
Console.WriteLine(arr.Count);
foreach (string s in new System.Collections.ArrayList(arr))
{
arr.Remove(s);
}
Console.WriteLine(arr.Count);
Console.ReadKey();
}
}
}
--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET - MS Press
Professional VSTO 2005 - Wrox/Wiley
OWC Black Book www.lulu.com/owc

"bbg" <bb*@discussions.microsoft.comwrote in message
news:0F**********************************@microsof t.com...
>Hi all,

I wanted to go through each entry(?) of ArrayList and remove some
particular
entry. So I tried following but it throws exception at runtime:

foreach (myEntry entry in myArrayList)
{
// do something...

if (entry.fieldA == 0)
{ // remove entry
myArrayList.Remove(entry); // exception below
} // remove entry
}

/* exception:
System.InvalidOperationException: Collection was modified; enumeration
operation may not execute.
at System.Collections.ArrayList.ArrayListEnumeratorSi mple.MoveNext()
*/

So I had to create an index array to remove and remove indexed entries
outside of foreach() loop. Is this a natural way to do or is there a
better
way?

Regards

Bob


Aug 29 '07 #7
Sure, but it's the most inefficient solution posted yet.
Did you care to test the code before making that statement?

If you did, you'd find that the "inefficiency" is not noticable for 10,000
items being removed from the collection - at least on my lap top. And that
isn't even a real world scenario anyway. You should be far more concerned
with inefficiencies and performance issues due to network bandwidth; SQL
queries; start up times; and obscure counters to track and flag items for
deletion before railing about inefficient code.

if you care to throw in systems programming as a reason for your efficiency
comment, then you wouldn't even be using arraylists in the first place as
OPs code showed.

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET - MS Press
Professional VSTO 2005 - Wrox/Wiley
OWC Black Book www.lulu.com/owc

"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:OB**************@TK2MSFTNGP03.phx.gbl...
>
"Alvin Bruney [MVP]" <some guy without an email addresswrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>Nope, there's an easier way to do this without the hocus pocus. Here is
the proof of concepts:

Sure, but it's the most inefficient solution posted yet.
>>
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
System.Collections.ArrayList arr = new
System.Collections.ArrayList();
arr.Add("1");
arr.Add("2");
arr.Add("3");

/*This throws an exception
foreach (string s in arr)
{
arr.Remove(s);
}
*/

//where as this works like a charm
Console.WriteLine(arr.Count);
foreach (string s in new System.Collections.ArrayList(arr))
{
arr.Remove(s);
}
Console.WriteLine(arr.Count);
Console.ReadKey();
}
}
}
--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET - MS Press
Professional VSTO 2005 - Wrox/Wiley
OWC Black Book www.lulu.com/owc

"bbg" <bb*@discussions.microsoft.comwrote in message
news:0F**********************************@microso ft.com...
>>Hi all,

I wanted to go through each entry(?) of ArrayList and remove some
particular
entry. So I tried following but it throws exception at runtime:

foreach (myEntry entry in myArrayList)
{
// do something...

if (entry.fieldA == 0)
{ // remove entry
myArrayList.Remove(entry); // exception below
} // remove entry
}

/* exception:
System.InvalidOperationException: Collection was modified; enumeration
operation may not execute.
at System.Collections.ArrayList.ArrayListEnumeratorSi mple.MoveNext()
*/

So I had to create an index array to remove and remove indexed entries
outside of foreach() loop. Is this a natural way to do or is there a
better
way?

Regards

Bob



Aug 30 '07 #8

"Alvin Bruney [MVP]" <some guy without an email addresswrote in message
news:eA**************@TK2MSFTNGP02.phx.gbl...
>Sure, but it's the most inefficient solution posted yet.
Did you care to test the code before making that statement?
I don't need to. It makes one additional memory copy vs the "naive"
solution of using indexes instead of an iterator.

That single copy of the list plus the need to test every element is already
more expensive than the straightforward, highly efficient solution of making
a new list with just the items not removed, and then your solution also
calls Remove a number of times, which not only does a linear search to find
the index of the element being removed, it also then moves the other part of
the list to close the gap.
>
If you did, you'd find that the "inefficiency" is not noticable for 10,000
items being removed from the collection - at least on my lap top. And that
isn't even a real world scenario anyway. You should be far more concerned
with inefficiencies and performance issues due to network bandwidth; SQL
queries; start up times; and obscure counters to track and flag items for
deletion before railing about inefficient code.
There's no reason to write an inefficient version when the framework already
provides List<T>.RemoveAll(Predicate<T>). Premature optimization deals with
rolling your own instead of using an existing function. What you suggested
was premature de-optimization -- rolling your own and ending up with worse
performance than the existing one.
Aug 30 '07 #9
<"Alvin Bruney [MVP]" <some guy without an email address>wrote:
Your code does one O(n) pass for each element which needs to be
removed. Big difference.
Yup, big difference - they both remove 10,000 items in the collection in
well under a second. Pretty big difference eh? I'll venture a guess that OPs
container holds less than 100 items. What are we saving here? Is this
noticeable in production code? Let's not throw around terms like
'inefficient' when the context is unclear. These terms only serve to scare
developers. If I were reviewing that application for performance, I wouldn't
even focus on that piece of code, there's no bang for developer bucks
optimizing away milliseconds.
You seemed to be considering your code to be linear, that's all. One
part is linear, but that's repeated a number of times.

Is it a significant inefficiency? That depends on the size of list, and
wasn't what I was addressing.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 30 '07 #10
I found this way(iterate backwards) was best for me and it worked well.
Thanks Nicholas.
Bob
"Nicholas Paldino [.NET/C# MVP]" wrote:
Or, if you want to prevent having to make two iterations, you could just
iterate backwards through the list (so you can consistently traverse the
items in the list as you remove them) and remove the items as needed:

for (int index = myArrayList.Count - 1; index >= 0; index--)
{
// Get the item.
myEntry entry = (myEntry) myArrayList[index];

// Check to remove.
if (entry.fieldA == 0)
{
// Remove.
myArrayList.RemoveAt(index);
}
}
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.comwrote in
message news:%2****************@TK2MSFTNGP06.phx.gbl...
Hi,

"bbg" <bb*@discussions.microsoft.comwrote in message
news:0F**********************************@microsof t.com...
Hi all,

I wanted to go through each entry(?) of ArrayList and remove some
particular
entry. So I tried following but it throws exception at runtime:

foreach (myEntry entry in myArrayList)
{
// do something...

if (entry.fieldA == 0)
{ // remove entry
myArrayList.Remove(entry); // exception below
} // remove entry
}

/* exception:
System.InvalidOperationException: Collection was modified; enumeration
operation may not execute.
at System.Collections.ArrayList.ArrayListEnumeratorSi mple.MoveNext()
*/

So I had to create an index array to remove and remove indexed entries
outside of foreach() loop. Is this a natural way to do or is there a
better
way?
You cannot modify the collection you are iterating in. You have to keep
track of those elements that you want removed and remove them after the
colection is iterate. A way of doing it is
ArrayList toremove = new ...
foreach (myEntry entry in myArrayList)
{
// do something...

if (entry.fieldA == 0)
toremove.Add( entry)
}
foreach(myEntry entry in toremove )
myArrayList.Remove( entry);



Sep 6 '07 #11

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

Similar topics

2
by: Stephen | last post by:
I have an array list alSearchCriteria which contains values which vary each time its created. It will always have 15 items in the arraylist each time its created. Some of the values in the array...
6
by: Nikhil Patel | last post by:
Hi all, Following is a portion of an XML document. I need to remove all nodes that belong to ns0 without deleting their child nodes. So in the following example , I want to delete "ns0:Proposal"...
2
by: steve smith | last post by:
Hi I am new to this language, and still don't understand most of the concepts, I am trying to sort an arraylist by alphabet, I belive i need to use the IComparer interface, but have no idea how...
11
by: steve smith | last post by:
Hi I'm still having some problems getting my head round this language. A couple of things don't seem to work for me. First I am trying to obtan a count of the number of words in a sting, so am...
7
by: Franck Diastein | last post by:
Hi, I'm trying to remove items from a collection this way: foreach(Object myO in ObjectCol){ if(myO != "xxx"){ myO.Remove(); } } But I'm having an error telling me that Collection was...
11
by: Robert W. | last post by:
Here's some pseudo-code that describes what I'm trying to do: foreach(object in collection) { if (certain-condition) collection.Remove(object); } The problem with this is that foreach gets...
10
by: pamelafluente | last post by:
Hi I have a sorted list with several thousands items. In my case, but this is not important, objects are stored only in Keys, Values are all Nothing. Several of the stored objects (might be a...
3
by: Martin Arvidsson, Visual Systems AB | last post by:
Hi! I am going crayz, i cant get this to work. and i don't know what the problem is. I have this method public ArrayList ResolveData() { ArrayList workingList = new ArrayList();
4
by: tshad | last post by:
I have an ArrayList of thread pointers that I want delete from my list when the Thread in not alive and status is stopped. But I want to do it from the foreach loop I am looping through. ...
0
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
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
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...
1
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,...
1
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
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.