TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

VB.NET ArrayList Examples

These VB.NET examples use the ArrayList type, a resizable array. They add, remove and get elements.

ArrayList. This collection is a dynamic array.

It resizes to fit new elements. An array type offers superior performance. But the ArrayList is sized automatically by built-in code.

Functions. ArrayList has many functions that help manage a linear collection. We add objects. We remove and insert things. And we count the number of elements.

Add example. First the Add subroutine is often used. It appends the object argument to the end of the internal ArrayList data structure. This program adds three elements to the ArrayList.

Tip: You do not need to check to see if there is room before adding the element.

Tip 2: There will always be room except in extreme circumstances such as out-of-memory situations.

Based on:

.NET 4.5

VB.NET program that uses Add, ArrayList

Module Module1

    Sub Main()
	' Create a new ArrayList.
	' ... Then add three strings to it.
	Dim list As New ArrayList
	list.Add("One")
	list.Add("Two")
	list.Add("Three")
    End Sub

End Module

Parameter. It is possible and often useful to receive an ArrayList as a parameter to a Sub. We can specify it as a parameter with "As ArrayList." The syntax is simple.

Tip: The Example method here could be used with any ArrayList instance, with any elements in its internal storage.

VB.NET program that uses ArrayList with method

Module Module1

    Sub Main()
	' Create an ArrayList and add two elements to it.
	Dim list As New ArrayList
	list.Add(5)
	list.Add(7)
	' Use ArrayList as an argument to the method.
	Example(list)
    End Sub

    ''' <summary>
    ''' Receives ArrayList as argument.
    ''' </summary>
    Private Sub Example(ByVal list As ArrayList)
	Dim num As Integer
	For Each num In list
	    Console.WriteLine(num)
	Next
    End Sub

End Module

Output

5
7

AddRange. It is possible to add a range of elements from one ArrayList onto the end of another ArrayList. To do this, please consider using the AddRange Sub.

Argument: AddRange receives one argument—an ArrayList that contains elements you want to add.

Here: In this example, the two array lists are effectively concatenated. This is done with the AddRange Sub.

VB.NET program that uses AddRange method

Module Module1

    Sub Main()
	' Create an ArrayList and add two elements.
	Dim list1 As New ArrayList
	list1.Add(5)
	list1.Add(7)
	' Create a separate ArrayList.
	Dim list2 As New ArrayList
	list2.Add(10)
	list2.Add(13)
	' Add this ArrayList to the other one.
	list1.AddRange(list2)
	' Loop over the elements.
	Dim num As Integer
	For Each num In list1
	    Console.WriteLine(num)
	Next
    End Sub

End Module

Output

5
7
10
13

Count, Clear. Often with ArrayList, you will not be sure how many elements are in the current instance. Fortunately, the ArrayList offers the Count property.

Count: This is a property. Count quickly returns the number of elements in the ArrayList.

Property

Clear: This example uses the Clear method. After you call the Clear method, the Count property will return zero elements.

VB.NET program that uses ArrayList and Count property

Module Module1

    Sub Main()
	' Add two elements to the ArrayList.
	Dim list As New ArrayList
	list.Add(9)
	list.Add(10)
	' Write the Count.
	Console.WriteLine(list.Count)
	' Clear the ArrayList.
	list.Clear()
	' Write the Count again.
	Console.WriteLine(list.Count)
    End Sub

End Module

Output

2
0

Insert, Remove. We show how to use the Add, RemoveAt, Insert, and RemoveRange methods. We have already seen the Add method in the first example.

RemoveAt: We see how the RemoveAt method works. It receives an index argument, which corresponds to the element index you want to remove.

Insert: The Insert method receives two arguments: the position you want to insert at, and the object reference itself.

RemoveRange: Finally, RemoveRange receives the index you want to start removing at, and the number of elements you want to remove.

VB.NET program that uses Add, RemoveAt, Insert, RemoveRange

Module Module1

    Sub Main()
	' Create an ArrayList and add three strings to it.
	Dim list As New ArrayList
	list.Add("Dot")
	list.Add("Net")
	list.Add("Perls")
	' Remove a string.
	list.RemoveAt(1)
	' Insert a string.
	list.Insert(0, "Carrot")
	' Remove a range.
	list.RemoveRange(0, 2)
	' Display.
	Dim str As String
	For Each str In list
	    Console.WriteLine(str)
	Next
    End Sub

End Module

Output

Perls

TryCast. In the ArrayList, elements are not directly stored with a type. Instead they are accessed through the object base type. To cast an object to a more derived type, use TryCast.

Operator: TryCast receives two arguments: the element we want to cast from the ArrayList, and the type to which we want to cast.

Warning: The TryCast operator will not throw exceptions, as it uses the tester-doer pattern.

TryCast

VB.NET program that uses TryCast, ArrayList

Module Module1

    Sub Main()
	' Create a new ArrayList.
	Dim list As New ArrayList
	list.Add("man")
	list.Add("woman")
	list.Add("plant")
	' Loop over the ArrayList with a For-loop.
	Dim i As Integer
	For i = 0 To list.Count - 1
	    ' Cast to a string.
	    Dim str As String = TryCast(list.Item(i), String)
	    Console.WriteLine(str)
	Next i
    End Sub

End Module

Output

man
woman
plant

GetRange. Here we extract one part of an ArrayList into another. To do this, please use the GetRange method on the original ArrayList instance.

Then: Assign the result of the GetRange method call to a new ArrayList variable reference.

Info: GetRange receives the starting index from which you want to copy, and then the count of elements you want to get.

Integer

VB.NET that uses ArrayList and GetRange

Module Module1

    Sub Main()
	' Create new ArrayList.
	Dim list1 As New ArrayList
	list1.Add("fish")
	list1.Add("amphibian")
	list1.Add("bird")
	list1.Add("plant")
	' Create a new ArrayList.
	' ... Fill it with the range from the first one.
	Dim list2 As New ArrayList
	list2 = list1.GetRange(2, 2)
	' Loop over the elements.
	Dim str As String
	For Each str In list2
	    Console.WriteLine(str)
	Next
    End Sub

End Module

Output

bird
plant

Hashtable. The ArrayList is similar to the Hashtable. For newer generic collections we have the List and Dictionary types. These are often used together in programs.

Hashtable

A consideration. There is rarely a need to use ArrayList in modern programs. Instead the List is used. It is faster and has clearer syntax (once you get past the generic declarations).

List

A core class. We examined the ArrayList type. This is a core class in the .NET Framework. It is harder to use in VB.NET due to the casting syntax required. But older programs often need it.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf