ArrayList subtract?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Craig Buchanan

    ArrayList subtract?

    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


  • drew

    #2
    Re: ArrayList subtract?

    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" <someone@micros oft.com> wrote in message
    news:%231dRGMDG EHA.3568@tk2msf tngp13.phx.gbl. ..[color=blue]
    > 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[/color]
    the[color=blue]
    > 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
    >
    >[/color]


    Comment

    • drew

      #3
      Re: ArrayList subtract?

      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" <someone@micros oft.com> wrote in message
      news:%231dRGMDG EHA.3568@tk2msf tngp13.phx.gbl. ..[color=blue]
      > 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[/color]
      the[color=blue]
      > 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
      >
      >[/color]


      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: ArrayList subtract?

        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("thr ee")

        assigned.Add("t wo")

        For Each group As Object In groups
        If not assigned.Contai ns(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" <someone@micros oft.com> wrote in message
        news:%231dRGMDG EHA.3568@tk2msf tngp13.phx.gbl. ..[color=blue]
        > 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[/color]
        the[color=blue]
        > 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
        >
        >[/color]


        Comment

        • Jay B. Harlow [MVP - Outlook]

          #5
          Re: ArrayList subtract?

          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("thr ee")

          assigned.Add("t wo")

          For Each group As Object In groups
          If not assigned.Contai ns(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" <someone@micros oft.com> wrote in message
          news:%231dRGMDG EHA.3568@tk2msf tngp13.phx.gbl. ..[color=blue]
          > 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[/color]
          the[color=blue]
          > 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
          >
          >[/color]


          Comment

          Working...