PC Review


Reply
Thread Tools Rate Thread

ArrayList subtract?

 
 
Craig Buchanan
Guest
Posts: n/a
 
      1st Apr 2004
I have two arraylists, one with a list of groups and one with a list of
groups assigned to a user. I would like to subtract the second AL from the
first AL to get an AL of groups to which the user *isn't* assigned. Is
there an easy way to do this?

Thanks,

Craig


 
Reply With Quote
 
 
 
 
drew
Guest
Posts: n/a
 
      1st Apr 2004
yes, but i'd suggest you move to another data structure such as the dataset
with two datatables which will allow you to manage relationships much easier
than looping over two arraylists in a nested for loop. one issue if you
decide to go the second route it you have to store the elements you wish to
remove in a third list and then iterate that list to remove from the second
list.... because you can't modify the underlying list while using an
ienumerator base on it. datasets look a lot better dont they?


"Craig Buchanan" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> I have two arraylists, one with a list of groups and one with a list of
> groups assigned to a user. I would like to subtract the second AL from

the
> first AL to get an AL of groups to which the user *isn't* assigned. Is
> there an easy way to do this?
>
> Thanks,
>
> Craig
>
>



 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      2nd Apr 2004
Craig,
As Drew suggested, create a third AL that is your result, for each item in
the first, if it is not in the second add it to your result.

Something like:

Dim groups As New ArrayList
Dim assigned As New ArrayList
Dim result As New ArrayList

groups.Add("one")
groups.Add("two")
groups.Add("three")

assigned.Add("two")

For Each group As Object In groups
If not assigned.Contains(group) Then
result.Add group
End If
Next

Instead of an ArrayList however I would use a GroupCollection object that
contains individual Group objects, where GroupCollection inherits from
either DictionaryBase or CollectionBase depending on whether it is a "keyed"
collection or not. The above code would be a function of the GroupCollection
object.

Public Class GroupCollection
Inherits CollectionBase

...

Public Function GetDifference(...) ...
For Each group As Object In InnerList


Hope this helps
Jay

"Craig Buchanan" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> I have two arraylists, one with a list of groups and one with a list of
> groups assigned to a user. I would like to subtract the second AL from

the
> first AL to get an AL of groups to which the user *isn't* assigned. Is
> there an easy way to do this?
>
> Thanks,
>
> Craig
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thread-safety: Change property of items in arraylist versus removingitems from the arraylist Curious Microsoft Dot NET 2 6th Aug 2008 12:36 PM
How to access ArrayList values inside another ArrayList? Pavel Maly Microsoft C# .NET 6 30th Oct 2006 01:46 PM
arraylist inside an arraylist for datagrid, gridview rjl Microsoft C# .NET 4 13th Apr 2006 07:32 PM
ArrayList(ICollection) constructor & overriden ArrayList.AddRange(). Sylvain Microsoft C# .NET 1 4th Jun 2005 01:19 AM
a class inherited from ArrayList, is saved to ViewState, why the type of the object read from ViewSate is not the class, but the parent, ArrayList leal ting Microsoft ASP .NET 0 29th Dec 2003 07:08 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:42 PM.