Figure 2.1
Output of Listing 2.1.1 when viewed through a browser.
Adding Elements to an ArrayList
In Listing 2.1.1 we create two ArrayList class instances, aTerritories and aStates, on lines 5 and 6, respectively. We then populate the aStates ArrayList with a small subset of the 50 states of the United States using the Add method (lines 9 through 13). The Add method takes one parameter, the element to add to the array, which needs to be of type Object. This Object instance is then appended to the end of the ArrayList. In this example we are simply adding elements of type String to the ArrayList aStates and aTerritories.
The Add method is useful for adding one element at a time to the end of the array, but what if we want to add a number of elements to an ArrayList at once? The ArrayList class provides the AddRange method to do just this. AddRange expects a single parameter that supports the ICollection interface. A wide number of .NET Framework classes—such as the Array, ArrayList, DataView, DataSetView, and others—support this interface. On line 18 in Listing 2.1.1, we use the AddRange method to add each element of the aStates ArrayList to the end of the aTerritories ArrayList. (To add a range of elements starting at a specific index in an ArrayList, use the InsertRange method.) On lines 18 and 19, we add two more strings to the end of the aTerritories ArrayList.
Because ArrayLists are ordered sequentially, there might be times when we want to add an element to a particular position. The Insert method of the ArrayList class provides this capability, allowing the developer to add an element to a specific spot in the ArrayList collection. The Insert method takes two parameters: an integer representing the index in which you want to add the new element, and the new element, which needs to be of type Object. In line 23 we add a new string to the start of the aTerritories ArrayList. Note that if we had simply used the Add method, "District of Columbia" would have been added to the end of aTerritories. Using Insert, however, we can specify exactly where in the ArrayList this new element should reside.
Removing Elements from an ArrayList
The ArrayList class also provides a number of methods for removing elements. We can remove a specific element from an ArrayList with the Remove method. On line 37 we remove the String "Wyoming" from the aTerritories ArrayList. (If you attempt to remove an element that does not exist, an ArgumentException exception will be thrown.) Remove allows you to take out a particular element from an ArrayList; RemoveAt, used on line 40, allows the developer to remove an element at a specific position in the ArrayList.
Both Remove and RemoveAt dissect only one element from the ArrayList at a time. We can remove a chunk of elements in one fell swoop by using the RemoveRange method. This method expects two parameters: an index to start at and a count of total elements to remove. In line 56 we remove the first two elements in aTerritories with the statement: aTerritories.RemoveRange(0, 2). Finally, to remove all the contents of an ArrayList, use the Clear method (refer to Line 69 in Listing 2.1.1).
Referencing ArrayList Elements
Note that in our code example, we used two different techniques to iterate through the contents of our ArrayList. Because an ArrayList stores items sequentially, we can iterate through an ArrayList by looping from its lowest bound through its upper bound, referencing each element by its integral index. The following code snippet is taken from lines 30 through 33 in Listing 2.1.1:
For i = 0 to aTerritories.Count - 1
lblTerritories.Text = lblTerritories.Text & _
aTerritories(i) & "<br>"
Next
The Count property returns the number of elements in our ArrayList. We start our loop at 0 because