ArrayLists are not used like standard arrays. Normally, items are just added to an ArrayList using the Add method, unless there is a reason why an item should be added at a particular position, in which case the Insert method should be used. In this section, we examine how to use these and the other members of the ArrayList class.
The first thing we have to do with an ArrayList is declare it, as we do here: Dim grades As New ArrayList()
Notice that a constructor is used in this declaration. If an ArrayList is not declared using a constructor, the object will not be available in later program statements.
Objects are added to an ArrayList using the Add method. This method takes a single argument—an Object to add to the ArrayList. The Add method also returns an integer indicating the position in the ArrayList where the element was added, though this value is rarely used in a program. Here are some examples:
grades.Add(100) grades.Add(84)
The ArrayList Class 61
Dim position As Integer position = grades.Add(77)
Console.WriteLine("The grade 77 was added at " & _ "position: " & position)
The objects in an ArrayList can be displayed using a For Each loop. The ArrayList has a built-in enumerator that manages iterating through all the ob- jects in the ArrayList, one at a time. The following code fragment demonstrates how to use a For Each loop with an ArrayList:
Dim grade As Object Dim total As Integer = 0 Dim average As Double = 0.0 For Each grade In grades
total += CInt(grade) Next
average = total / grades.Count
Console.WriteLine("The average grade is: " & average) If you want to add an element to an ArrayList at a particular position, you can use the Insert method. This method takes two arguments: the index to insert the element and the element to be inserted. The following code fragment inserts two grades in specific positions to preserve the order of the objects in the ArrayList:
grades.Insert(1, 99) grades.Insert(3, 80)
You can check the current capacity of an ArrayList by calling the Capacity property and you can determine how many elements are in an ArrayList by calling the Count property:
Console.WriteLine("The current capacity of grades is:" _ & grades.Capacity)
Console.WriteLine("The number of grades in grades is:" _ & grades.Count)
There are several ways to remove items from an ArrayList. If you know the item you want to remove, but don’t know its position, you can use
the Remove method. This method takes just one argument—an object to remove from the ArrayList. If the object exists in the ArrayList, it is removed. If the object isn’t in the ArrayList, nothing happens. When a method like Remove is used, it is typically called inside an If–Then statement using a method that can verify whether the object is actually in the ArrayList, such as the Contains method. Here’s a sample code fragment:
If (grades.Contains(54)) Then grades.Remove(54)
Else
MsgBox("Object not in ArrayList.") End If
If you know the index of the object you want to remove, you can use the RemoveAt method. This method takes one argument—the index of the object you want to remove. The only exception you can cause is passing an invalid index to the method. The method works like this:
grades.RemoveAt(2)
You can determine the position of an object in an ArrayList by calling the IndexOf method. This method takes one argument, an object, and returns the object’s position in the ArrayList. If the object is not in the ArrayList, the method returns −1. Here’s a short code fragment that uses the IndexOf method in conjunction with the RemoveAt method:
Dim pos As Integer
pos = grades.IndexOf(70) grades.RemoveAt(pos)
In addition to adding individual objects to an ArrayList, you can also add ranges of objects. The objects must be stored in a data type that is derived from ICollection. This means that the objects can be stored in an array, a Collection, or even in another ArrayList.
There are two different methods you can use to add a range to an ArrayList: AddRange and InsertRange. The AddRange method adds the range of objects to the end of the ArrayList, and the InsertRange method adds the range at a specified position in the ArrayList.
The ArrayList Class 63
The following program demonstrates how these two methods are used: Module Module1
Sub Main()
Dim names As New ArrayList names.Add("Mike")
names.Add("Bernica") names.Add("Beata") names.Add("Raymond") names.Add("Jennifer") Dim name As Object
Console.WriteLine("The original list of names") For Each name In names
Console.WriteLine(name) Next
Console.WriteLine()
Dim newNames() As String = {"David", "Michael"} Dim moreNames As New Collection
moreNames.Add("Terrill") moreNames.Add("Donnie") moreNames.Add("Mayo") moreNames.Add("Clayton") moreNames.Add("Alisa") names.InsertRange(0, newNames) names.AddRange(moreNames)
Console.WriteLine("The new list of names") For Each name In names
Console.WriteLine(name) Next
Console.Read() End Sub
End Module
The output from this program is as follows: David
Michael Mike Bernica
Beata Raymond Jennifer Terrill Donnie Mayo Clayton Alisa
The first two names are added at the beginning of the ArrayList because the specified index is 0. The last names are added at the end because the AddRange method is used.
Two other methods that many programmers find useful are the ToArray method and the GetRange method. The GetRange method returns a range of objects from the ArrayList as another ArrayList. The ToArray method copies all the elements of the ArrayList to an array. Let’s look first at the GetRange method.
The GetRange method takes two arguments: the starting index and the number of elements to retrieve from the ArrayList. GetRange is not destruc- tive, in that the objects are just copied from the original ArrayList into the new ArrayList. Here’s an example of how the method works, using our same program:
Dim someNames As New ArrayList someNames = names.GetRange(2, 4)
Console.WriteLine("someNames sub-ArrayList") For Each name In someNames
Console.WriteLine(name) Next
The output from this program fragment is as follows: Mike
Bernica Beata Raymond
The ToArray method allows you to easily transfer the contents of an ArrayList to a standard array. The primary reason for using the ToArray method would be to take advantage of the faster access speed of an array.