indy500fan
asked on
Looking for a better way of searching an ArrayList
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.V alidAMBMLP String
Dim SplitUpString() As String
SplitUpString = Split(AMBMLPValidString, "¦")
If SplitUpString.GetUpperBoun d(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 (AMBMLPVal idString, 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 (AMBMLPVal idString, 61)
e_Driver_LastName = AMBParser_Entrant.GetValue (AMBMLPVal idString, 62)
Dim DriverEntrantProperty As New DriverEntrant
DriverEntrantProperty.CarN umber = e_CarNumber
DriverEntrantProperty.Firs tName = e_Driver_FirstName
DriverEntrantProperty.Last Name = e_Driver_LastName
DriverEntrantAL.Add(Driver EntrantPro perty)
End If
Thanks in advance!
Regards,
Eric
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.V
Dim SplitUpString() As String
SplitUpString = Split(AMBMLPValidString, "¦")
If SplitUpString.GetUpperBoun
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
Dim DriverEntrantCarNumberTemp
For Each oDriverEntrant In DriverEntrantAL
DriverEntrantCarNumberTemp
If e_CarNumber = DriverEntrantCarNumberTemp
CarNumberExistsInList = True
Exit For
Else
CarNumberExistsInList = False
End If
Next
If CarNumberExistsInList = False Then
e_Driver_FirstName = AMBParser_Entrant.GetValue
e_Driver_LastName = AMBParser_Entrant.GetValue
Dim DriverEntrantProperty As New DriverEntrant
DriverEntrantProperty.CarN
DriverEntrantProperty.Firs
DriverEntrantProperty.Last
DriverEntrantAL.Add(Driver
End If
Thanks in advance!
Regards,
Eric
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
Any other suggestions?
Thanks,
Eric
Hi Eric;
How about using the e_CarNumber & "-" & e_Driver_LastName as the key?
Fernando
How about using the e_CarNumber & "-" & e_Driver_LastName as the key?
Fernando
ASKER
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
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
No problem, glad to help. ;=)
ASKER
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
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
Not a problem, Thanks. ;=)
Sorry I didn't check back earlier for the duplicate key fiasco. Glad you got an answer that works for you :)
ASKER
ZeonFlash,
No worries, I just hope you're okay with the points distribution.
No worries, I just hope you're okay with the points distribution.
ASKER
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!