[RESOLVED] [02/03] ArrayList Search
How do you search an arraylist?
The code below skips the first value of the arraylist for some reason.
TokenNotLoaded = ArrayList that i'm trying to fill
Code:
TokenNotLoaded.Sort()
''check to see if the token is already loaded into the array
Dim idx As Integer = TokenNotLoaded.BinarySearch(sToken, New CaseInsensitiveComparer)
If idx > 0 Then
Else
TokenNotLoaded.Add(tokenMatch.ToString)
End If
Re: [02/03] ArrayList Search
BinarySearch returns a Zero-based index if the item is found,
otherwise the return value is negative.
So you should change your If expression to include Zero.
VB Code:
TokenNotLoaded.Sort()
' Check to see if the token is already loaded into the array
Dim idx As Integer = TokenNotLoaded.BinarySearch(sToken, New CaseInsensitiveComparer)
If idx >= 0 Then
' Do Nothing
Else
TokenNotLoaded.Add(tokenMatch.ToString)
End If
Regards,
- Aaron.
Re: [02/03] ArrayList Search
All arrays and collections are zero-based in .NET, so zero is always the index of the first item/element. Also, having an If...Else statement with only one of the blocks used is a bit silly. Unless you actually intend to add somethong else just reverse the condition:
VB Code:
If idx < 0 Then
TokenNotLoaded.Add(tokenMatch.ToString())
End If