I’ve wrote a small array wrapper class which mimics the built-in Collection for adding and retrieving items. Once the object is created, you set the array capacity and that would not change.
'// ArrayList Class Private arr() As String Private position As Long ' Add Public Sub Add(ByVal Value As String) arr(position) = Value position = position + 1 End Sub ' Count Public Function Count() As Long Count = UBound(arr) End Function ' Item Public Function Item(ByVal Index As Long) As String Item = arr(Index) End Function ' SetCapacity Public Sub SetCapacity(ByVal arrsize As Long) ReDim arr(arrsize) End Sub
Testing the object:
Sub TestArrayList() Dim a As ArrayList Set a = New ArrayList a.SetCapacity 4 a.Add "One" a.Add "Two" a.Add "Three" a.Add "Four" a.Add "Five" Dim i As Long For i = 0 To a.Count Debug.Print a.Item(i) Next i End Sub 'Output as expected 'One 'Two 'Three 'Four 'Five
It is widely known that expensive operations of an array are
ReDim Preserve where a new array is created and the items of the old array are being copied.
In the object above, we only create a new array when calling the
SetCapacity(arrsize) method. No need to preserve any items. Thus the expensive operation here is the creation of the new array (fixed size) which is only performed once.
The question in terms of speed and overhead is: would a built-in collection be more appropriate than the above wrapper object?
The aim is to create a Collection of objects but I'm not sure which would be more appropriate, a
Collection(Of Collection) or a
In terms of data, the ArrayList will hold around 16 elements and the Collection around 1,500 objects.