To add a new item to an ArrayList, use the Add method, whose syntax is as follows:
index = aList.Add(obj)
aList is a properly declared ArrayList, and obj is the item you want to add to the ArrayList collection (it could be a number, a string, or an object). The Add method appends the specified item to the collection and returns the index of the new item. If you’re using an ArrayList named Capitals to store the names of the state capitals, you can add an item by using the following statement:
Capitals.Add("Sacramento")
Code language: JavaScript (javascript)
If the Persons ArrayList holds variables of a custom type, prepare a variable of that type and then add it to the collection. Let’s say you created a structure called Person by using the following declaration:
Structure Person
Dim LastName As String
Dim FirstName As String
Dim Phone As String
Dim EMail As String
End Structure
Code language: JavaScript (javascript)
To store a collection of Person items in an ArrayList, create a variable of the Person type, set its fields, and then add it to the ArrayList, as shown in Listing 10.3.
Listing 10.3: Adding a Structure to an ArrayList
Dim Persons As New ArrayList
Dim p As New Person
p.LastName = "Last Name"
p.FirstName = "First Name"
p.Phone = "Phone"
p.EMail = "[email protected]"
Persons.Add(p)
p = New Person
p.LastName = "another name"
{ statements to set the other fields}
Persons.Add(p)
Code language: PHP (php)
If you execute these statements, the ArrayList will hold two items, both of the Person type. Notice that you can add multiple instances of the same object to the ArrayList collection. To find out whether an item belongs to the collection already, use the Contains method, which accepts as an argument an object and returns a True or False value, depending on whether the object belongs to the list:
If Persons.Contains(p) Then
MsgBox("Duplicate element rejected")
Else
Persons.Add(p)
MsgBox("Element appended successfully")
End If
Code language: PHP (php)
By default, items are appended to the ArrayList. To insert an item at a specific location, use the Insert method, which accepts as an argument the location at which the new item will be inserted and, of course, an object to insert in the ArrayList, as shown next:
aList.Insert(index, object)
Code language: CSS (css)
Unlike the Add method, the Insert method doesn’t return a value — the location of the new item is already known.
You can also add multiple items via a single call to the AddRange method. This method appends a collection of items to the ArrayList. These items could come from an array or from another ArrayList. The following statement appends the elements of an array to the aList collection:
Dim colors() As Color = {Color.Red, Color.Blue, Color.Green}
aList.AddRange(colors)
Code language: PHP (php)
The AddRange method in this example appends three items of the same type to the aList collection. The array could have been declared as Object, too; it doesn’t have to be strongly typed because the ArrayList collection is not strongly typed.
To insert a range of items anywhere in the ArrayList, use the InsertRange method. Its syntax is the following, where index is the index of the ArrayList where the new elements will be inserted, and objects is a collection of the elements to be inserted:
aList.InsertRange(index, objects)
Code language: CSS (css)
Finally, you can overwrite a range of elements in the ArrayList with a new range by using the SetRange method. To overwrite the items in locations 5 through 9 in an ArrayList, use a few statements like the following:
Dim words() As String = {"Just", "a", "few", "more", "words"}
aList.SetRange(5, words)
Code language: JavaScript (javascript)
This code segment assumes that the aList collection contains at least 10 items, and it replaces half of them.
To remove an item, use the Remove method, whose syntax is the following:
aList.Remove(object)
Code language: CSS (css)
The object argument is the value to be removed, not an index value. If the collection contains multiple instances of the same item, only the first instance of the object will be removed. Notice that the Remove method compares values, not references. If the ArrayList contains a Rectangle object, you can search for this item by creating a new Rectangle variable and setting its properties to the properties of the Rectangle object you want to remove:
Dim R1 As New Rectangle(10, 10, 100, 100)
Dim R2 As Rectangle = R1
aList.Add(R1)
aList.Add(R2)
Dim R3 As Rectangle
R3 = New Rectangle(10, 10, 100, 100)
aList.Remove(R3)
Code language: PHP (php)
If you execute these statements, they will add two identical rectangles to the aList ArrayList. The last statement will remove the first of the two rectangles.
If you attempt to remove an item that doesn’t exist, no exception is thrown— simply, no item is removed from the list. You can also remove items by specifying their index in the list via the RemoveAt method. This method accepts as an argument the index of the item to remove, which must be less than the number of items currently in the list.
To remove more than one consecutive item, use the RemoveRange method, whose syntax is the following:
aList.RemoveRange(startIndex, count)
Code language: CSS (css)
The startIndex argument is the index of the first item to be removed, and count is the number of items to be removed.
The following statements are examples of the methods that remove items from an ArrayList collection. The first two statements remove an itemby value. The first statement removes an object, and the second removes a string item. The following statement removes the third item, and the last one removes the third through the fifth items.
aList.Remove(Color.Red)
aList.Remove("Richard")
aList.RemoveAt(2)
aList.RemoveRange(2, 3)
Code language: CSS (css)
Extracting Items from an ArrayList
To access the items in the ArrayList, use an index value, similar to the array. The first item’s index is 0 and the last item’s index is AL.Count-1, where AL is a properly initialized ArrayList collection. You can also extract a range of items from the list by using the GetRange method. This method extracts a number of consecutive elements from the ArrayList and stores them to a new ArrayList, where index is the index of the first item to copy, and count is the number of items to be copied:
newList = ArrayList.GetRange(index, count)
The GetRange method returns another ArrayList with the proper number of items. The following statement copies three items from the aList ArrayList and inserts them at the beginning of the bList ArrayList. The three elements copied are the fourth through sixth elements in the original collection:
bList.InsertRange(0, aList.GetRange(3, 3))
Code language: CSS (css)
The Repeat method, which fills an ArrayList with multiple instances of the same item, has the following syntax:
newList = ArrayList.Repeat(item, count)
This method returns a new ArrayList with count elements, all of them being identical to the item argument.
Another method of the ArrayList class is the Reverse method, which reverses the order of the elements in an ArrayList collection, or a portion of it.