JustAnswer.com

Thursday, February 22, 2007

Asp.Net Tutorials

and makeup of the array. With all the collections we’ll examine in this chapter, this is
no
longer a concern. Adding, Removing, and Indexing Elements in an ArrayList The ArrayList class contains a number of methods for adding and removing Objects from the collection. These include Add, AddRange, Insert, Remove, RemoveAt, RemoveRange, and Clear, all of which we’ll examine in Listing 2.1. The output is shown in Figure 2.1. LISTING 2.1 For Sequentially Accessed Collections, Use the ArrayList 2: 3: Sub Page_Load(source as Object, e as EventArgs) 4: ‘ Create two ArrayLists, aTerritories and aStates 5: Dim aTerritories as New ArrayList 6: Dim aStates as New ArrayList 7: 8: ‘ Use the Add method to add the 50 states of the US 9: aStates.Add(“Alabama”) 10: aStates.Add(“Alaska”) 11: aStates.Add(“Arkansas”) 12: ‘ ... 13: aStates.Add(“Wyoming”)
15: ‘ Build up our list of territories, which includes
16: ‘ all 50 states plus some additional countries 17: aTerritories.AddRange(aStates) ‘ add all 50 states 18: aTerritories.Add(“Guam”) 19: aTerritories.Add(“Puerto Rico”) 20: 21: ‘ We’d like the first territory to be the District of Columbia, 22: ‘ so we’ll explicitly add it to the beginning of the ArrayList 23: aTerritories.Insert(0, “District of Columbia”) 24: 25: ‘ Display all of the territories with a for loop 26: lblTerritories.Text = “There are “ & aTerritories.Count & _ 27: “territories...” 28: 29: Dim i as Integer 30: For i = 0 to aTerritories.Count - 1 31: lblTerritories.Text = lblTerritories.Text & _ 32: aTerritories(i) & “”