Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Looking for a better way of searching an ArrayList

Posted on 2006-10-23
11
Medium Priority
?
203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 17

Assisted Solution

by:ZeonFlash
ZeonFlash earned 1000 total points
ID: 17792140
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 64

Accepted Solution

by:
Fernando Soto earned 1000 total points
ID: 17792221
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
ID: 17795105
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:indy500fan
ID: 17795436
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 64

Expert Comment

by:Fernando Soto
ID: 17795633
Hi Eric;

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

Fernando
0
 

Author Comment

by:indy500fan
ID: 17795791
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 64

Expert Comment

by:Fernando Soto
ID: 17795888
No problem, glad to help. ;=)
0
 

Author Comment

by:indy500fan
ID: 17796444
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 64

Expert Comment

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

Expert Comment

by:ZeonFlash
ID: 17796859
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
ID: 17796896
ZeonFlash,

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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 …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

604 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