Solved

Looking for a better way of searching an ArrayList

Posted on 2006-10-23
11
152 Views
Last Modified: 2010-04-23
Friends,

Right now, I have a TCP Feed that is spitting out records.  Now, one of the records contains entrant information.  I am taking out selected entrant info and putting it into an arraylist.

Seems simple enough.  Now, the entrant information is repeated every so often, and if I already have it in my arraylist, I don't need to re-add it.  So, right now, here is my code to see if the information exists in the array.  If not, it adds it.  My question is, is this the best way (going through the list to see if a match is found), or is there a better way?

Here is where the ArrayLists are defined:

    Dim DriverEntrantAL As New ArrayList
    Dim oDriverEntrant As DriverEntrant

This is where the DriverEntrant Property List is defined:

Public Class DriverEntrant
    Public CarNumber As String
    Public FirstName As String
    Public LastName As String
End Class

Here is where I look to see if it is in the arraylist (and if not I add it.):

Public Sub ProcessValidString(ByVal AMBMLPValidString As String) 'Handles RecieveValidAMBMLPString.ValidAMBMLPString

        Dim SplitUpString() As String
        SplitUpString = Split(AMBMLPValidString, "¦")

        If SplitUpString.GetUpperBound(0) = 0 Then
            Exit Sub
        End If

        Dim Record As String
        Record = SplitUpString(0)


        Select Case Record

            Case "E"

                'If CarNumber doesn't exist in array, add (3) values to array as record
                e_CarNumber = AMBParser_Entrant.GetValue(AMBMLPValidString, 4) 'Just know that this returns the CarNumber of this particular record...
                Dim DriverEntrantCarNumberTemp As String = Nothing

                For Each oDriverEntrant In DriverEntrantAL

                    DriverEntrantCarNumberTemp = oDriverEntrant.CarNumber
                    If e_CarNumber = DriverEntrantCarNumberTemp Then
                        CarNumberExistsInList = True
                        Exit For
                    Else
                        CarNumberExistsInList = False
                    End If
                Next

                If CarNumberExistsInList = False Then

                    e_Driver_FirstName = AMBParser_Entrant.GetValue(AMBMLPValidString, 61)
                    e_Driver_LastName = AMBParser_Entrant.GetValue(AMBMLPValidString, 62)

                    Dim DriverEntrantProperty As New DriverEntrant

                    DriverEntrantProperty.CarNumber = e_CarNumber
                    DriverEntrantProperty.FirstName = e_Driver_FirstName
                    DriverEntrantProperty.LastName = e_Driver_LastName
                    DriverEntrantAL.Add(DriverEntrantProperty)
                End If


Thanks in advance!

Regards,
Eric
0
Comment
Question by:indy500fan
  • 5
  • 4
  • 2
11 Comments
 
LVL 17

Assisted Solution

by:ZeonFlash
ZeonFlash earned 250 total points
Comment Utility
You could try using a HashTable instead of an ArrayList.  Ofcourse, this assumes that your "CarNumber" ID will ALWAYS be unique.  You could then add the CarNumber as the Key and the instance of the DriverEntrant class as the Value.  Kind of like this:

Dim DriverEntrantHT As New HashTable

Dim newDriver As New DriverEntrant
newDriver.CarNumber = 3
newDriver.FirstName = "John"
newDriver.LastName = "Doe"

DriverEntrantHT.Add(newDriver.CarNumber, newDriver)

The best part about using a HashTable is the searching and retrieval.  You can simply search if the value has already been added by doing this:

If Not DriverEntrantHT.ContainsKey(Car_Number_To_Check) Then
  'The Driver has not been entered
End If

And if you want to retrieve the class instance:

Dim theDriver As DriverEntrant = DriverEntrantHT("3")
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 250 total points
Comment Utility
You can use a Hash Table

    Dim DriverEntrantHT As New Hashtable

        ' Your search routine will become
        CarNumberExistsInList = False
        If DriverEntrantHT.ContainsKey(oDriverEntrant.CarNumber) Then
            CarNumberExistsInList = True
        End If

        If CarNumberExistsInList = False Then

            e_Driver_FirstName = AMBParser_Entrant.GetValue(AMBMLPValidString, 61)
            e_Driver_LastName = AMBParser_Entrant.GetValue(AMBMLPValidString, 62)

            Dim DriverEntrantProperty As New DriverEntrant

            DriverEntrantProperty.CarNumber = e_CarNumber
            DriverEntrantProperty.FirstName = e_Driver_FirstName
            DriverEntrantProperty.LastName = e_Driver_LastName

            ' Add info to Hashtable
            DriverEntrantHT.Add(e_CarNumber, DriverEntrantProperty)

        End If


Fernando
0
 

Author Comment

by:indy500fan
Comment Utility
Zeon and Fernando,

Hey, I didn't think of that!  That's why I love this forum.  I'm going to try both of your solutions, and then I'll assign points.

Thank you!
0
 

Author Comment

by:indy500fan
Comment Utility
Hmm...In thinking about this, there is a possiblity that two drivers will have the same car number.  For example, an experienced driver might get in the car to "shake it down" for a rookie.  In this instance, you would have two drivers with the same car number, or key.  

Any other suggestions?

Thanks,
Eric
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Hi Eric;

How about using the e_CarNumber & "-" & e_Driver_LastName as the key?

Fernando
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:indy500fan
Comment Utility
Fernando,

That concept would work, but unfortunately, I'll have to do e_CarNumber & "-" & e_Driver_LastName & "-" & e_Driver_FirstName, because last 500 we had a father and son drive the same car.  Though something like that is rare, that might work.  Thanks Fernando!

Eric
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
No problem, glad to help. ;=)
0
 

Author Comment

by:indy500fan
Comment Utility
Okay guys, I'm going to split the points 50/50, and here's why:

ZeonFlash - You answered first (which usually gives me the indication who gets the majority of points); however, Fernando gave me code that I could cut and paste into my project, and followed up on a clarification question.

I hope that's fair to both of you!

Regards,
Eric
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Not a problem, Thanks. ;=)
0
 
LVL 17

Expert Comment

by:ZeonFlash
Comment Utility
Sorry I didn't check back earlier for the duplicate key fiasco.  Glad you got an answer that works for you :)
0
 

Author Comment

by:indy500fan
Comment Utility
ZeonFlash,

No worries, I just hope you're okay with the points distribution.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now