Collections
  Iterate over a collection
  Use a Hashtable collection
  Choose a collection to use
  Implement a collection
  Clone a collection

Get URL for this page

How Do I...Iterate over a collection?

This topic illustrates how to create and use an ArrayList. An important part of this discussion is how to use the foreach (For Each in VB) command to loop through the list. An ArrayList is a member of the Collection namespace, and can accept any type of object as its elements. This topic also covers the methods and properties of the ArrayList object.

 
VB List.aspx

[Run Sample] | [View Source]

Collections are very similar to arrays. On the whole, collections can be viewed as specialized arrays, with a few more methods and properties to help you out. In this demonstration, you will work with one particular collection, ArrayList.

ArrayList can take all kinds of objects, but when working with collections, it is beneficial to try to use only one data type. The first piece of sample code you are going to look at is creating the list. This is an easy, one line piece of code in which you use the constructor for ArrayList to make a new instance of this collection. Unlike an array, there is no need to figure out how many elements the list is going to take: the ArrayList can do all that for you as you add elements to it.


'  declare the list, with a default capacity of 16
Dim al as New ArrayList()
VB

When you make an ArrayList, it has no elements in it, but it has the ability to accept up to 16 elements. If you try to add a 17th element, the ArrayList helps you out by doubling its capacity. This can be useful to know because there is a certain performance hit associated with increasing the collection in this way. If you know how many elements your collection will need, you can set the capacity in advance, saving on that performance issue (however small).

To prove that the default capacity of ArrayList is greater than 16, you need to ask ArrayList what its capacity is, and how many elements it has. You can do this by accessing properties of the same name on the list.


'  display the initial count, and capacity. 0 and 16
Console.WriteLine("The initial Count of our list is {0} " & _
		"and its Capacity is {1}", al.Count, al.Capacity)
VB

Try this out yourself to make sure you agree. You can set the capacity property, as long as you don't go below the number of elements currently in the list, or below zero. Count is read-only, since it reflects the actual number of elements in the list, which is determined by how many you add.

If you want to add items to the list, use the Add method, which takes any object as an argument and places that object at the end of the list. If you want to place an object at a particular place in the list, use the Insert method, specifying the index to place the new object at, and the object to insert.

Not only can you add elements to your list, you can also take elements away from it. To do this, use the Remove method, specifying the object to remove (you should use an object reference to do this), or the RemoveAt method, passing the index of the element to remove. The following code example demonstrates adding and removing objects.


'  declare an Integer object, to add to the list
dim intTemp As Integer = 12

' add elements to the list
al.Add("Apple")
al.Add(intTemp)
al.Add( New MyType() )  '  a specific object we made ourselves

Console.WriteLine("Removing the integer object...")
'  remove a specific element, using the Remove method
al.Remove(intTemp)

Console.WriteLine("Removing the MyType object...")
'  remove a specific object, using RemoveAt
al.RemoveAt(1)  '  remove the MyType. Remember, we just removed intTemp,
		'  which reduced all element indexes by 1.

Console.WriteLine("Inserting new object...")
'  add an object at a specific location
al.Insert(0, "Grapes")

' display the count, and capacity
Console.WriteLine("After modifying our list, Count is {0} " & _
		"and Capacity is {1}", al.Count, al.Capacity)
VB

Notice that the only objects left in the list after running the above code are strings. This is going to help with the foreach statement. After making the ArrayList, you can iterate through that list by making a temporary object that can point at each element of the list in turn. The data type of the temporary 'pointing' object can help you out. Because you only have Strings, you can make it a String object. If you are uncertain, use a generic Object. After you have printed out the elements in the list, use the Clear method to remove all the elements in the list. The following code example demonstrates these concepts.


'  note that because we know only Strings are left in our list,
'  we can use a String object in our For Each statement. If you
'  are at all uncertain, use For Each objItem in al
Dim strItem As String

for Each strItem in al
	Console.WriteLine(strItem)
Next strItem

'  remove all elements from the list
al.Clear()
VB

Remember that an ArrayList has many other useful methods and properties that can help you manipulate your list. Use the .NET Framework SDK to determine how these elements work.


Copyright 2001-2002 Microsoft Corporation. All rights reserved.