ArrayList subtract?

  • Thread starter Thread starter Craig Buchanan
  • Start date Start date
C

Craig Buchanan

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
 
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,
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
 
Back
Top