Visual Basic .NET - Dynamic Array

In VB.NET, dynamic arrays are arrays that can be resized during runtime. They are also known as dynamic memory allocation, which means that memory is allocated as needed while the program is running. Unlike static arrays, the size of a dynamic array can be changed at runtime. Dynamic arrays are implemented using the ArrayList class in VB.NET.

Declaring a Dynamic Array:

To declare a dynamic array in VB.NET, you need to use the ArrayList class. Here is an example of how to declare a dynamic array:

Dim dynamicArray As New ArrayList()

Adding Elements to Dynamic Array:

You can add elements to a dynamic array using the Add method of the ArrayList class. Here is an example:

dynamicArray.Add("John")
dynamicArray.Add("Jane")

Accessing Elements in Dynamic Array:

You can access elements in a dynamic array using the indexing operator. Here is an example:

Dim firstElement As String = dynamicArray(0)
Dim secondElement As String = dynamicArray(1)

Resizing a Dynamic Array:

To resize a dynamic array, you need to use the Resize method of the ArrayList class. Here is an example:

dynamicArray.Resize(3)

This will resize the dynamic array to three elements.

Removing Elements from Dynamic Array:

To remove an element from a dynamic array, you need to use the Remove method of the ArrayList class. Here is an example:

dynamicArray.Remove("John")

This will remove the element "John" from the dynamic array.

Iterating through a Dynamic Array:

You can use the For Each loop to iterate through a dynamic array. Here is an example:

For Each element As String In dynamicArray
    Console.WriteLine(element)
Next

This will print all the elements in the dynamic array to the console.

Storing User Input: You can create a dynamic array to store user input values. For example, you can prompt the user to enter the number of elements to be stored in the array, and then use a loop to get the values of each element.

Dim size As Integer
Console.Write("Enter the size of the array: ")
size = Integer.Parse(Console.ReadLine())
Dim arr(size - 1) As Integer
For i As Integer = 0 To size - 1
    Console.Write("Enter element {0}: ", i + 1)
    arr(i) = Integer.Parse(Console.ReadLine())
Next
Console.WriteLine("The array elements are:")
For Each elem In arr
    Console.Write(elem & " ")
Next

Removing Duplicates: You can also use dynamic arrays to remove duplicates from an existing array. Here's an example:

Dim arr() As Integer = {1, 2, 2, 3, 4, 4, 5}
Dim unique() As Integer = {}
For Each elem In arr
    If Not unique.Contains(elem) Then
        Array.Resize(unique, unique.Length + 1)
        unique(unique.Length - 1) = elem
    End If
Next
Console.WriteLine("The array without duplicates is:")
For Each elem In unique
    Console.Write(elem & " ")
Next

Sorting: You can use dynamic arrays to sort an existing array in ascending or descending order. Here's an example:

Dim arr() As Integer = {5, 3, 8, 1, 2}
Array.Sort(arr)
Console.WriteLine("The array in ascending order is:")
For Each elem In arr
    Console.Write(elem & " ")
Next
Array.Reverse(arr)
Console.WriteLine(vbCrLf & "The array in descending order is:")
For Each elem In arr
    Console.Write(elem & " ")
Next

Advantages of Dynamic Arrays:

  • Dynamic arrays can be resized during runtime.
  • They are more flexible than static arrays.
  • They can be used to store objects of different types.
  • Dynamic arrays can save memory by only allocating memory as needed.

Disadvantages of Dynamic Arrays:

  • They can be slower than static arrays due to dynamic memory allocation.
  • They can use more memory than static arrays due to dynamic memory allocation.
  • Dynamic arrays can be more complex to work with than static arrays.