# [.net] Arraylist remove_if?

## Recommended Posts

JimPrice    815
If I want to cycle through an ArrayList and remove all objects meeting a predicate condition, how do I do this? I can see some horrid ways of doing it, but surely there's a simple way? Thanks, Jim.

##### Share on other sites
Washu    7829
No, there is no RemoteIf functionality, you would have to provide that yourself. It's not particularily hard to write though.

	delegate bool RemoveCondition(object value);	class ArrayUtility {		public void RemoveIf(IList array, RemoveCondition predicate) {			if (array == null)				throw new ArgumentNullException("array");			if (predicate == null)				throw new ArgumentNullException("predicate");			int i = 0;			while (i < array.Count) {				if (predicate(array[i])) {					array.RemoveAt(i);				} else {					++i;				}			}		}	}

##### Share on other sites
JimPrice    815
Obvious really.

I was thinking of the same thing, but using foreach - but then you can't use RemoveAt because it invalidates the enumerator. Only found out whilst playing with something else in the last few minutes that you access an ArrayList using operator[] - forgot the analogy to std::vector.

Thanks very much,
Jim.

##### Share on other sites
turnpast    1011
You are probalby not working in dotnet 2.0 if you are using ArrayList, but you may be in the future so you may find this of interest:

As of 2.0, Array, List<T> and some of the other collection classes contain methods for just this sort of operation. RemoveAll and FindAll take an exclusion/inclusion predicate delegate and produce a sublist probably in just the way you want.

Also note that calling RemoveAt() again and again on an array list could be quite inefficent, probably better to make a copy add the desired elements and delete the orignal list when done.

##### Share on other sites
JimPrice    815
Quote:
 You are probalby not working in dotnet 2.0 if you are using ArrayList

Unfortunately, I'm working with 1.1 (because I'm a lazy-arse who'd rather just use their VS.Pro package as opposed to downloading the new version).

One thing I'm really missing is generics - the number of times I've written pretty much the same enumerator code in this project is really quite depressing.

Quote:
 As of 2.0, Array, List and some of the other collection classes contain methods for just this sort of operation. RemoveAll and FindAll take an exclusion/inclusion predicate delegate and produce a sublist probably in just the way you want.

Yeah, I figured they probably would. With 1.1 I miss the STL-type algorithms on occasion.

Quote:
 Also note that calling RemoveAt() again and again on an array list could be quite inefficent, probably better to make a copy add the desired elements and delete the orignal list when done.

That was pretty much my original thought. Interesting to hear the comment on efficiency though. I'm only removing occasionally, from a short list, so that might affect your feelings on efficiency.

Jim.

##### Share on other sites
turnpast    1011
Quote:
 Original post by JimPriceThat was pretty much my original thought. Interesting to hear the comment on efficiency though. I'm only removing occasionally, from a short list, so that might affect your feelings on efficiency.

Yup:
occasionally & short == no big deal either way.

##### Share on other sites
mutex    1111
Quote:
 Original post by JimPriceOne thing I'm really missing is generics - the number of times I've written pretty much the same enumerator code in this project is really quite depressing.
Plus if you ever have to implement an enumerator there are new statements yield return and yield break that greatly simplify the process. <3 C# 2.0.

##### Share on other sites
DrGUI    402
Quote:
Original post by JimPrice
Quote:
 You are probalby not working in dotnet 2.0 if you are using ArrayList

Unfortunately, I'm working with 1.1 (because I'm a lazy-arse who'd rather just use their VS.Pro package as opposed to downloading the new version).

Me too because to download the beta you need an expensive MSDN subscription.

##### Share on other sites
Sijmen    231
Quote:
 Me too because to download the beta you need an expensive MSDN subscription.

Not really.. you can either order the DVD with the pro version beta for a few dollars, or download the Express editions for free from labs.msdn.microsoft.com/express (iirc).