System.Collections.ArrayList

An arraylist is a combination of an array and a collection.
legacy only used for backwards compatibility
Upgrade to Generic.List<T>





This allows you to work with a set of values as if they were an array and a collection at the same time.
An arraylist has the following functionality:

  • You can address a value by its index

  • You can quickly sort and reverse the list

  • You can search sequentially

  • You can append elements to the end and insert them in a particular position

  • You can remove any element

  • Expands automatically as more elements are added (just like a collection)


Imagine that you need to keep track of various objects but you have no idea how many.
It is difficult to use an array for this purpose because you must declare the size of the array at compile time
The ArrayList is a class designed specifically for this situation
This is an aray whose size is dynamically increased as required.



Initailising ArrayLists

The default capacity for a new arraylist is 16 elements.

Dim aliArrayListIntegers As New System.Collections.ArrayList 

You should always try and define an initial capacity for your arraylist if possible.
This defines an arraylist with an initial capacity of 200 elements.

Dim aliArrayListIntegers  As New System.Collections.ArrayList(200) 

You can modify the capacity at any time by assigning a value to the Capacity property

aliArrayListIntegers.Capacity = aliArrayListInteger.Count 

Another way to achieve the same result is to use the TrimToSize property

aliArrayListIntegers.TrimToSize 

When the current capacity is exceeded the ArrayList will automatically double its capacity.


Default Elements

It is possible to initialise an arraylist and to poulate it with an initial value

Dim aliArrayListIntegers  As New System.Collections.ArrayList.Repeat(-1,200) 
Dim alsArrayListStrings As New System.Collections.ArrayList.Repeat("",200)

Properties/Methods

The ArrayList class fully implements the IList interface with all the familiar methods.

AddAppends an element after the last element in the collection and returns the index where it was inserted.
CapacityProperty contains the number of elements the srray can currently hold.
ClearRemoves all elements from the collection.
ContainsReturns TRUE if an element exists in the collection.
CopyToCopies the elements from the collection to a new one-dimensional array, starting at the specified index in the array.
CountReturns the number of elements in the collection.
GetEnumeratorMethod that returns an enumerator to iterate an ArrayList
IndexOfReturns the index of the element in the collection, or -1 if it cannot be found.
InsertInserts an element at the specified index.
IsFixedSizeReturns TRUE if no more elements can be added to the collection.
IsReadOnlyReturns TRUE is the elements cannot be altered.
ItemGets or sets the elements at the specified 0-based index.
RemoveRemoves an element from the collection
RemoveAtRemoves an element at the specified index from the collection
ReverseMethod that reverses the order of the elements
SortMethod that alphabetically sorts the elements
ToArrayMethod that copies all the elements to a new one-dimensional array


Do not return Nothing

Return New System.Collections.ArrayList 

Does an Element Exist

If (alArrayList.Contains(sElement) = True) Then 

End If


Removing an Element

The Remove method will only remove the first occurrence of an element.

alAuthors.Remove("") 

Also this method does not generate an exception if the element you want to remove cannot be found.
There are two ways you can remove all the instances of an element.


IndexOf

This method is very concise but not very efficient.
You can also use the contain method.

Do While alsArrayListStrings.IndexOf("remove this") >= 0 
   alsArrayListStrings.Remove("remove this")
Loop

Count

This is far more efficient, this loops until the count property becomes constant.

Dim isavecount As Integer 
Do
   isavecount = alsArrayListStrings.Count
   alsArrayListStrings.Remove("remove this")
Loop While (alsArrayListStrings.Count < isavecount)


Converting to a String Array

Dim arValues() As String 
Dim arArrayList As System.Collections.ArrayList

arValues = DirectCast(arArrayList.ToArray(GetType(String)),String())




© 2018 Better Solutions Limited. All Rights Reserved. © 2018 Better Solutions Limited TopPrevNext