By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,831 Members | 1,031 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,831 IT Pros & Developers. It's quick & easy.

remove ArrayList[n] inside foreach() loop?

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a

"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

P: n/a
<"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.