How to VB.NET ArrayList

The ArrayList is a highly flexible data structure within VB.NET Collections. It offers dynamic array-like functionality, allowing you to add elements to the array dynamically. One of the key advantages of using an ArrayList is its ability to accept null as a valid value and allow duplicate elements within the collection.

While other Collection classes provide access to items using both numeric indexes and String keys, the ArrayList specifically supports access solely through numeric indexes. This means you can retrieve items from the ArrayList by specifying the position or index of the item in the collection.

One of the standout features of the ArrayList is its flexibility in terms of size management. Unlike traditional arrays that require predetermined size information, the ArrayList allows you to add items without prior knowledge of the collection's size. This dynamic sizing capability eliminates the need for explicitly resizing the collection as new items are added.

vb-arraylist

Important functions from ArrayList Object

The ArrayList object in VB.NET provides a range of important functions that facilitate efficient data manipulation and management. Some of the key functions of the ArrayList object include:

  1. Add: The Add function allows you to add elements to the end of the ArrayList. It dynamically increases the size of the collection to accommodate new elements.
  2. Remove: The Remove function enables you to remove a specific element from the ArrayList based on its value. It eliminates the element from the collection and adjusts the size accordingly.
  3. Insert: The Insert function allows you to insert an element at a specific index within the ArrayList. It shifts the existing elements to accommodate the new element.
  4. Clear: The Clear function clears all the elements from the ArrayList, making it empty. It resets the size of the collection to zero.
  5. Count: The Count function returns the number of elements present in the ArrayList. It provides a convenient way to determine the size of the collection.
  6. Contains: The Contains function checks whether a specific element is present in the ArrayList. It returns a Boolean value indicating the presence or absence of the element.
  7. IndexOf: The IndexOf function returns the index of the first occurrence of a specific element within the ArrayList. It allows you to determine the position of an element in the collection.
  8. Sort: The Sort function arranges the elements in the ArrayList in ascending order. It provides a convenient way to sort the elements based on their values.
  9. Reverse: The Reverse function reverses the order of elements in the ArrayList. It allows you to change the sequence of elements within the collection.
  10. ToArray: The ToArray function converts the ArrayList into a regular array, allowing you to access elements using index-based notation.

How to add Items in an ArrayList ?

Syntax : ArrayList.add(Item) Item : The Item to be add the ArrayList
Dim ItemList As New ArrayList() ItemList.Add("Item4")

How to Insert Items in an ArrayList ?

  1. Syntax : ArrayList.insert(index,item).
  2. index : The position of the item in an ArrayList.
  3. Item : The Item to be add the ArrayList.
ItemList.Insert(3, "item6")

How to remove an item from arrayList ?

Syntax : ArrayList.Remove(item) Item : The Item to be add the ArrayList
ItemList.Remove("item2")

How to remove an item in a specified position?

Syntax : ArrayList.RemoveAt(index) index : the position of an item to remove from an ArrayList
ItemList.RemoveAt(2)

How to sort ArrayList ?

Syntax : ArrayListSort()

From the following Visual Basic source code you can see some important operations from an ArrayList Object

Full Source VB.NET
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim i As Integer Dim ItemList As New ArrayList() ItemList.Add("Item4") ItemList.Add("Item5") ItemList.Add("Item2") ItemList.Add("Item1") ItemList.Add("Item3") MsgBox("Shows Added Items") For i = 0 To ItemList.Count - 1 MsgBox(ItemList.Item(i)) Next 'insert an item ItemList.Insert(3, "Item6") 'sort itemms in an arraylist ItemList.Sort() 'remove an item ItemList.Remove("Item1") 'remove item from a specified index ItemList.RemoveAt(3) MsgBox("Shows final Items the ArrayList") For i = 0 To ItemList.Count - 1 MsgBox(ItemList.Item(i)) Next End Sub End Class

Conclusion

These functions, among others, empower you to manipulate and manage data effectively within the ArrayList object. By using these functions, you can add, remove, insert, search, and perform various other operations on the elements contained in the ArrayList with ease and flexibility.