Visual Basic Arraylist

In visual basic, Arraylist is useful to store elements of different data types and the size of arraylist can grow or shrink dynamically based on the need of our application like adding or removing elements from arraylist.

 

In visual basic, arraylists are same as an arrays but the only difference is arrays are used to store a fixed number of same data type elements.

Visual Basic Arraylist Declaration

In visual basic, arraylist is a non-generic type of collection and it is provided by System.Collections namespace.

 

As discussed, collection is a class so to define an arraylist, we must need to declare an instance of the arraylist class before we perform any operations like add, delete, etc. like as shown below.

 

Dim arrlist As ArrayList = New ArrayList()

If you observe the above arraylist declaration, we created a new arraylist (arrList) with an instance of arraylist class without specifying any size.

Visual Basic Arraylist Properties

Following are the some of commonly used properties of an arraylist in visual basic programming language.

 

PropertyDescription
Capacity It is used to get or set the number of elements an arraylist can contain.
Count It is used to get the number of elements in arraylist.
IsFixedSize It is used to get a value to indicate that the arraylist has fixed size or not.
IsReadOnly It is used to get a value to indicate that the arraylist is readonly or not.
Item It is used get or set an element at the specified index.
IsSynchronized It is used to get a value to inidicate that an access to arraylist is synchronized (thread safe) or not.

Visual Basic Arraylist Methods

Following are the some of commonly used methods of an arraylist to add, search, insert, delete or sort an elements of arraylist in visual basic programming language.

 

MethodDescription
Add It is used to add an element at the end of arraylist.
AddRange It is used to add all the elements of specified collection at the end of arraylist.
Clear It will remove all the elements in arraylist.
Clone It will create a shallow copy of arraylist.
Contains It is used determine whether the specified element exists in arraylist or not.
CopyTo It is used to copy all the elements of an arraylist into another compatible array.
GetRange It is used to return a specified range of arraylist elements starting from the specified index.
IndexOf It is used to search for a specified element and return a zero-based index if found otherwise return -1 if no element found.
Insert It is used to insert an element in arraylist at the specified index.
InsertRange It is used to insert all the elements of specified collection into arraylist starting from the specified index.
Remove It is used to remove a first occurence of specified element from the arraylist.
RemoveAt It is used to remove an element from arraylist based on the specified index position.
RemoveRange It is used to remove a range of elements from arraylist.
Reverse It reverse the order of arraylist elements.
Sort It sorts the elements in an arraylist.
ToArray It copies all the elements of an arraylist to new array object.

Visual Basic Arraylist Example

Following is the example of using an arraylist in visual basic programming language.

 

Module Module1

    Sub Main(ByVal args As String())

        Dim arrlist As ArrayList = New ArrayList()

        arrlist.Add("Welcome")

        arrlist.Add(100)

        arrlist.Add(20.5)

        arrlist.Add("Tutlane")

        Console.WriteLine("ArrayList Count: " & arrlist.Count)

        Console.WriteLine("ArrayList Capacity: " & arrlist.Capacity)

        Console.WriteLine("*********ArrayList Elements********")

        For Each item In arrlist

            Console.WriteLine(item)

        Next

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we are able to define a new arraylist (arrlist) collection. Here, we are binding a different data type values to newly created arraylist (arrlist) by using Add method and with Count & Capacity properties we are able to access the number of elements in an arraylist.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic (VB) ArrayList Example Result

 

This is how we can use arraylist in visual basic to hold a group of different data type objects based on our requirements.

 

Following are the some of useful examples to work with arraylist in visual basic programming language.

Visual Basic Add Elements to ArrayList

In visual basic, by using the Add or AddRange method, we can add a different data type elements to an arraylist. 

 

Following is the example of adding elements to arraylist using Add or AddRange methods in visual basic.

 

Module Module1

    Sub Main(ByVal args As String())

        ' Creating arraylist

        Dim arrlist As ArrayList = New ArrayList()

        arrlist.Add("Welcome")

        arrlist.Add(100)

        arrlist.Add(20.5)

        arrlist.Add("Tutlane")

        ' Creating arraylist

        Dim arrlist2 As ArrayList = New ArrayList() From {10, "Hi"}

        ' Adding arrlist2 to arrlist

        arrlist.AddRange(arrlist2)

        Console.WriteLine("*********ArrayList Elements********")

        For Each item In arrlist

            Console.WriteLine(item)

        Next

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we created multiple arraylists (arrlist, arrlist2) and adding an elements to the arraylists in different ways.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic Add Elements to ArrayList Example Result

Visual Basic Access ArrayList Elements

In visual basic, we have different ways to access arraylist elements i.e. either by using index positions or by iterating through an arraylist using For / For Each loops.

 

Following is the example of accessing the arraylist elements in different ways.

 

Module Module1

    Sub Main(ByVal args As String())

        ' Creating ArrayList

        Dim arrlist As ArrayList = New ArrayList()

        arrlist.Add("Welcome")

        arrlist.Add(100)

        arrlist.Add(20.5F)

        arrlist.Add("Tutlane")

        ' Accessing first element

        Dim msg As String = CStr(arrlist(0))

        ' Accessing third element

        Dim num As Single = CSng(arrlist(2))

        Console.WriteLine("*********Access Elements with Index Position********")

        Console.WriteLine("Element at 0: " & msg)

        Console.WriteLine("Element at 2: " & num)

        Console.WriteLine("*********Access Elements with For Loop********")

        ' For loop to access arraylist elements

        For i As Integer = 0 To arrlist.Count - 1

            Console.WriteLine(arrlist(i))

        Next

        Console.WriteLine("*********Access Elements with Foreach Loop********")

        ' For Each loop to access ArrayList elements

        For Each item In arrlist

            Console.WriteLine(item)

        Next

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we are accessing the arraylist elements in different ways by using index positions, For and For Each loops based on our requirements.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic Access ArrayList Elements Example Result

Visual Basic Insert Elements into ArrayList

In visual basic, we can insert elements into arraylist either by using Insert() or InsertRange() methods.

 

Following is the example of inserting elements into arraylist by using Insert and InsertRange methods in visual basic.

 

Module Module1

    Sub Main(ByVal args As String())

        ' Creating arraylist

        Dim arrlist As ArrayList = New ArrayList()

        arrlist.Add("Welcome")

        arrlist.Add(100)

        arrlist.Add(20.5F)

        arrlist.Add("Tutlane")

        ' Inserting elements into arraylist

        arrlist.Insert(0, "Hi")

        arrlist.Insert(1, 50)

        Dim arrlist2 As ArrayList = New ArrayList() From {200, 300}

        ' Inserting arrlist2 into arrlist at position 2

        arrlist.InsertRange(2, arrlist2)

        Console.WriteLine("*********ArrayList Elements********")

        For Each item In arrlist

            Console.WriteLine(item)

        Next

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we inserted elements into arraylist at different index positions (0, 1) by using Insert() method. Same way, by using InsertRange() method we are inserting all the element of newly created arraylist (arrlist2) into arrlist at index position 2.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic Insert Elements into ArrayList Example Result

Visual Basic Remove Elements from ArrayList

In visual basic, we have different methods to remove elements from arraylist i.e. either by using Remove() or RemoveAt() or RemoveRange() methods.

 

Following is the example of deleting elements from arraylist in visual basic programming language.

 

Module Module1

    Sub Main(ByVal args As String())

        ' Creating arraylist

        Dim arrlist As ArrayList = New ArrayList()

        arrlist.Add("Welcome")

        arrlist.Add(100)

        arrlist.Add(20.5F)

        arrlist.Add("Tutlane")

        arrlist.Add(200)

        arrlist.Add(300)

        arrlist.Add(400)

        ' Removing an element which is having a value 20.5f

        arrlist.Remove(20.5F)

        ' Removing an element at index 0

        arrlist.RemoveAt(0)

        ' Removing 2 elements starting from index 3

        arrlist.RemoveRange(3, 2)

        Console.WriteLine("*********ArrayList Elements********")

        For Each item In arrlist

            Console.WriteLine(item)

        Next

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we used the Remove() method to delete a particular value of element from arraylist and used RemoveAt() method to delete an element at the specified index position.

 

Same way, we used RemoveRange() method to delete the specified number of elements from arraylist starting from the specified index position.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic Remove Elements from ArrayList Example Result

In case, if you want to remove all the elements from arraylist, then use Clear() method.

 

Same way, we can use different methods like Sort() method to sort an elements of arraylist and Reverse() method to reverse the order of arraylist elements.

Visual Basic ArrayList Check If Item Exists

 By using Contains() method, we can check whether the specified element exists in arraylist or not. In case, if it exists it will return True otherwise False.

 

Following is the example of using Contains() method to check for an item exists in the arraylist or not in visual basic.

 

Module Module1

    Sub Main(ByVal args As String())

        ' Creating arraylist

        Dim arrlist As ArrayList = New ArrayList()

        arrlist.Add("Welcome")

        arrlist.Add(100)

        arrlist.Add(20.5F)

        arrlist.Add("Tutlane")

        ' Check for an item 100 exists in arraylist or not

        Console.WriteLine("Item Exists: " & arrlist.Contains(100))

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we used Contains() method to check for an item (100) exists in the arraylist (arrlist) or not.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Item Exists: True

Visual Basic ArrayList Overview

Following are the important points which we need to remember about arraylist in visual basic.

 

  • Arraylist is useful to store elements of different data types and the size of arraylist can grow or shrink dynamically by adding or removing elements.
  • Arraylist is a non-generic type of collection and available with System.Collections namespace.
  • We can access arraylist elements either by using loops (For and For Each) or with particular index position.
  • We need to cast arraylist items to appropriate data types before we use it in our application.
  • Arraylist provided different methods to perform multiple operations like add, insert, delete, sort, reverse, etc. on elements of arraylist.

Visual Basic Array vs ArrayList

Following are the main difference between array and arraylist in visual basic programming language.

 

  • Array is a strongly typed which means it can store only the elements of the same data type but in arraylist, we can store elements of different data types.
  • The size of array is fixed whereas the size of arraylist will vary dynamically based on the requirements.
  • Arraylist provided different methods to perform operations like add, insert, remove, etc. on multiple elements but in array, we can get or set the value of only one element at a time.
  • In visual basic, the array can have multiple dimensions but the arraylist will have only one dimension.
  • To use an arraylist items, we need to cast it to appropriate data type but in the array we don’t need to cast an elements because it’s a strongly type.