ArrayList Search for Value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Veldthuis

    #1

    ArrayList Search for Value

    I have an ArrayList set up with a set of class objects.

    The class has 3 strings in it and are as follows

    ProdID
    GSP
    Description

    Okay now problem with getting all these into the list and displaying them correctly and manipulating
    things except for one thing.

    I wish to be able to do search through the whole list without stepping through the entire list
    enumerating them. I would just like to be able to search on one field. The ProdID.

    For now I do it by

    dim myspec as new clsSpecs
    for each myspec in specList
    if test.prodid.com pareto(myspec.P rodID) = 0 then

    do stuff

    end if

    next


    Now this works but I am looking for a search method but list.BinarySear ch does not seem to do it.

    Any help?
  • Peter van der Goes

    #2
    Re: ArrayList Search for Value


    "John Veldthuis" <johnv@no.spamo .paradise.net.n z> wrote in message
    news:man0l1lvf1 5433qpv1fiqkaad k1tei5ud7@4ax.c om...[color=blue]
    >I have an ArrayList set up with a set of class objects.
    >
    > The class has 3 strings in it and are as follows
    >
    > ProdID
    > GSP
    > Description
    >
    > Okay now problem with getting all these into the list and displaying them
    > correctly and manipulating
    > things except for one thing.
    >
    > I wish to be able to do search through the whole list without stepping
    > through the entire list
    > enumerating them. I would just like to be able to search on one field. The
    > ProdID.
    >
    > For now I do it by
    >
    > dim myspec as new clsSpecs
    > for each myspec in specList
    > if test.prodid.com pareto(myspec.P rodID) = 0 then
    >
    > do stuff
    >
    > end if
    >
    > next
    >
    >
    > Now this works but I am looking for a search method but list.BinarySear ch
    > does not seem to do it.
    >
    > Any help?[/color]

    I suggest you read this article:

    http://msdn.microsoft.com/library/de...archtopic1.asp

    You'll note that the objects in your ArrayList must come from a class that
    implements the IComparable interface, and that the objects must be sorted
    according to your IComparable implementation. Then the BinarySearch method
    will yield the results you seek.

    --
    Peter [MVP Visual Developer]
    Jack of all trades, master of none.



    Comment

    • John Veldthuis

      #3
      Re: ArrayList Search for Value

      On Fri, 14 Oct 2005 22:57:42 -0500, "Peter van der Goes" <p_vandergoes@t oadstool.u> wrote:

      Thanks. I had implemented IComparable to sort the list but was using it on the GSP entry.
      I changed it to look at the ProdID and it did return what I expected. Changed it back as that was
      what I needed the main sort on.

      However I added an IComparable called FindProdID and used the following

      myspec = new clsSpecs
      mySpec.ProdID = "100676"
      indx = specList.Binary Search(myspec, new FindProdID)

      and all worked well and returned the index it should have.

      Thanks for the pointer.
      [color=blue]
      >
      >"John Veldthuis" <johnv@no.spamo .paradise.net.n z> wrote in message
      >news:man0l1lvf 15433qpv1fiqkaa dk1tei5ud7@4ax. com...[color=green]
      >>I have an ArrayList set up with a set of class objects.
      >>
      >> The class has 3 strings in it and are as follows
      >>
      >> ProdID
      >> GSP
      >> Description
      >>
      >> Okay now problem with getting all these into the list and displaying them
      >> correctly and manipulating
      >> things except for one thing.
      >>
      >> I wish to be able to do search through the whole list without stepping
      >> through the entire list
      >> enumerating them. I would just like to be able to search on one field. The
      >> ProdID.
      >>
      >> For now I do it by
      >>
      >> dim myspec as new clsSpecs
      >> for each myspec in specList
      >> if test.prodid.com pareto(myspec.P rodID) = 0 then
      >>
      >> do stuff
      >>
      >> end if
      >>
      >> next
      >>
      >>
      >> Now this works but I am looking for a search method but list.BinarySear ch
      >> does not seem to do it.
      >>
      >> Any help?[/color]
      >
      >I suggest you read this article:
      >
      >http://msdn.microsoft.com/library/de...archtopic1.asp
      >
      >You'll note that the objects in your ArrayList must come from a class that
      >implements the IComparable interface, and that the objects must be sorted
      >according to your IComparable implementation. Then the BinarySearch method
      >will yield the results you seek.[/color]

      Comment

      • Cor Ligthert [MVP]

        #4
        Re: ArrayList Search for Value

        John,

        If you are sure that you want to use the complete word in the exact cases,
        than you can use the arraylist indexof method

        http://msdn.microsoft.com/library/de...dexoftopic.asp

        If you want it undependable from the cases than you should be able to use
        the Icomparer as nicely described by Peter Proost, Jay B. Harlow and more
        people in this newsgroup this week



        If you are the same John, than forget this message.

        I hope this helps,

        Cor


        Comment

        • Dennis

          #5
          Re: ArrayList Search for Value

          Don't you have to sort the arraylist on the ProdID property first before
          using the binarysearch?
          --
          Dennis in Houston


          "John Veldthuis" wrote:
          [color=blue]
          > On Fri, 14 Oct 2005 22:57:42 -0500, "Peter van der Goes" <p_vandergoes@t oadstool.u> wrote:
          >
          > Thanks. I had implemented IComparable to sort the list but was using it on the GSP entry.
          > I changed it to look at the ProdID and it did return what I expected. Changed it back as that was
          > what I needed the main sort on.
          >
          > However I added an IComparable called FindProdID and used the following
          >
          > myspec = new clsSpecs
          > mySpec.ProdID = "100676"
          > indx = specList.Binary Search(myspec, new FindProdID)
          >
          > and all worked well and returned the index it should have.
          >
          > Thanks for the pointer.
          >[color=green]
          > >
          > >"John Veldthuis" <johnv@no.spamo .paradise.net.n z> wrote in message
          > >news:man0l1lvf 15433qpv1fiqkaa dk1tei5ud7@4ax. com...[color=darkred]
          > >>I have an ArrayList set up with a set of class objects.
          > >>
          > >> The class has 3 strings in it and are as follows
          > >>
          > >> ProdID
          > >> GSP
          > >> Description
          > >>
          > >> Okay now problem with getting all these into the list and displaying them
          > >> correctly and manipulating
          > >> things except for one thing.
          > >>
          > >> I wish to be able to do search through the whole list without stepping
          > >> through the entire list
          > >> enumerating them. I would just like to be able to search on one field. The
          > >> ProdID.
          > >>
          > >> For now I do it by
          > >>
          > >> dim myspec as new clsSpecs
          > >> for each myspec in specList
          > >> if test.prodid.com pareto(myspec.P rodID) = 0 then
          > >>
          > >> do stuff
          > >>
          > >> end if
          > >>
          > >> next
          > >>
          > >>
          > >> Now this works but I am looking for a search method but list.BinarySear ch
          > >> does not seem to do it.
          > >>
          > >> Any help?[/color]
          > >
          > >I suggest you read this article:
          > >
          > >http://msdn.microsoft.com/library/de...archtopic1.asp
          > >
          > >You'll note that the objects in your ArrayList must come from a class that
          > >implements the IComparable interface, and that the objects must be sorted
          > >according to your IComparable implementation. Then the BinarySearch method
          > >will yield the results you seek.[/color]
          >[/color]

          Comment

          • John Veldthuis

            #6
            Re: ArrayList Search for Value

            On Sat, 15 Oct 2005 06:01:02 -0700, Dennis <Dennis@discuss ions.microsoft. com> wrote:
            [color=blue]
            >Don't you have to sort the arraylist on the ProdID property first before
            >using the binarysearch?[/color]

            Yes, I simply resorted using the new IComparer and then did the search and then resorted back the
            original.

            Comment

            • John Veldthuis

              #7
              Re: ArrayList Search for Value

              On Sat, 15 Oct 2005 11:08:58 +0200, "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote:
              [color=blue]
              >John,
              >
              >If you are sure that you want to use the complete word in the exact cases,
              >than you can use the arraylist indexof method
              >
              >http://msdn.microsoft.com/library/de...dexoftopic.asp
              >
              >If you want it undependable from the cases than you should be able to use
              >the Icomparer as nicely described by Peter Proost, Jay B. Harlow and more
              >people in this newsgroup this week
              >
              >http://groups.google.com/group/micro...9adf0d52e87dfc
              >
              >If you are the same John, than forget this message.
              >
              >I hope this helps,[/color]

              Thanks, different John but I understood all that. My problem was that I did not just have a simple
              entry as the example but a class and was unsure of how the binarysearch worked on only filling out
              part of the class. However all fixed now and working 100% how I expected thanks to the help and
              pointers from those here.

              Comment

              Working...