Like arrays, the ArrayList class exposes the IndexOf and LastIndexOf methods to search in an unsorted list and the BinarySearch method for sorted lists. The IndexOf and LastIndexOf methods accept as an argument the object to be located and return an index:
Dim index As Integer = aList.IndexOf(object)
Code language: PHP (php)
Here, object is the item you’re searching. The LastIndexOf method has the same syntax, but it starts scanning the array from its end and moves backward toward the beginning. The IndexOf and LastIndexOf methods are overloaded. The other two forms of the IndexOf method are these:
aList.IndexOf(object, startIndex)
aList.IndexOf(object, startIndex, length)
Code language: CSS (css)
The two additional arguments determine where the search starts and ends. Both methods return the index of the item if it belongs to the collection. If not, they return the value −1. The IndexOf and LastIndexOf methods of the ArrayList class perform case-sensitive searches, and they report exact matches only.
If the ArrayList is sorted, use the BinarySearch method, which accepts as an argument the object to be located and returns its index in the collection, where object is the item you’re looking for:
Dim index As Integer = aList.BinarySearch(object)
Code language: PHP (php)
There are two more forms of this method. To search for an item in an ArrayList with custom objects, use the following form of the BinarySearch method:
Dim index As Integer = aList.BinarySearch(object, comparer)
Code language: PHP (php)
The first argument is the object you’re searching for, and the second is the name of an IComparer object. Another form of the BinarySearch method allows you to search for an item in a section of the collection; its syntax is as follows:
Dim index As Integer = aList.BinarySearch(startIndex, length, object, comparer)
Code language: PHP (php)
The first argument is the index at which the search will begin, and the second argument is the length of the subrange. object and comparer are the same as with the second form of the method.
Iterating an ArrayList
To iterate through the elements of an ArrayList collection, you can set up a For. . .Next loop like the following one:
For i = 0 To aList.Count - 1
{ process item aList(i)}
Next
This is a trivial operation, but the processing itself can get as complicated as required by the type of objects stored in the collection. The current item at each iteration is the aList(i). It’s recommended that you cast the object to the appropriate type and then process it. You can also use the For Each. . .Next loop with an Object variable, as shown next:
Dim itm As Object
For Each itm In aList
{ process item itm}
Next
Code language: JavaScript (javascript)
If all the items in the ArrayList are of the same type, you can use a variable of this type to iterate through the collection, instead of a generic object variable. If all the elements are decimals, for example, you can declare the itm variable as Decimal.
An even better method is to create an enumerator for the collection and use it to iterate through its items. This technique applies to all collections and is discussed in the ‘‘Enumerating Collections” section later in this chapter.
The ArrayList class addresses most of the problems associated with the Array class, but one last problem remains — that of accessing the items in the collection through a meaningful key. This is the problem addressed by the HashTable collection.