15.2 Array ListsImagine that your program asks the user for input or gathers input from a web site. As it finds objects (strings, books, values, etc.), you would like to add them to an array, but you have no idea how many objects you'll collect in any given session. It is difficult to use an array for such a purpose because you must declare the size of an Array object at compile time. If you try to add more objects than you've allocated memory for, the Array class will throw an exception. If you do not know in advance how many objects your array will be required to hold, you run the risk of declaring either too small an array (and running out of room) or too large an array (and wasting memory). The .NET Framework provides a class designed for just this situation. The ArrayList class is an array whose size is dynamically increased as required. The ArrayList class provides many useful methods and properties. A few of the most important are shown in Table 15-3. Table 15-3. ArrayList members
When you create an ArrayList, you do not define how many objects it will contain. You add to the ArrayList using the Add( ) method, and the list takes care of its own internal bookkeeping, as illustrated in Example 15-2. Example 15-2. Using an ArrayListOption Strict On Imports System Namespace ArrayListDemo ' a class to hold in the array list Public Class Employee Private myEmpID As Integer Public Sub New(ByVal empID As Integer) Me.myEmpID = empID End Sub 'New Public Overrides Function ToString( ) As String Return myEmpID.ToString( ) End Function 'ToString Public Property EmpID( ) As Integer Get Return myEmpID End Get Set(ByVal Value As Integer) myEmpID = Value End Set End Property End Class 'Employee Class Tester Public Sub Run( ) Dim empArray As New ArrayList( ) Dim intArray As New ArrayList( ) ' populate the arraylists Dim i As Integer For i = 0 To 4 empArray.Add(New Employee(i + 100)) intArray.Add((i * 5)) Next i ' print each member of the array For Each i In intArray Console.Write("{0} ", i.ToString( )) Next i Console.WriteLine(ControlChars.Lf) ' print each employee Dim e As Employee For Each e In empArray Console.Write("{0} ", e.ToString( )) Next e Console.WriteLine(ControlChars.Lf) Console.WriteLine("empArray.Capacity: {0}", empArray.Capacity) End Sub 'Run Shared Sub Main( ) Dim t As New Tester( ) t.Run( ) End Sub 'Main End Class 'Tester End Namespace 'ArrayListDemo Output: 0 5 10 15 20 100 101 102 103 104 empArray.Capacity: 16 Suppose you're defining two ArrayList objects, empArray to hold Employee objects, and intArray to hold integers: Dim empArray As New ArrayList( ) Dim intArray As New ArrayList( ) Each ArrayList object has a property, Capacity, which is the number of elements the ArrayList is capable of storing.
You add elements to the ArrayList with the Add( ) method: empArray.Add(New Employee(i + 100)) intArray.Add((i * 5)) When you add the 17th element, the capacity is automatically doubled to 32. If you change the For loop to: For i = 0 To 17 the output looks like this: 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 empArray.Capacity: 32 Similarly, if you added a 33 rd element, the capacity would be doubled to 64. The 65 th element increases the capacity to 128, the 129th element increases it to 256, and so forth. |