Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Collections
- Module Program
- Sub Main()
- ' Create an ArrayList
- Dim myList As ArrayList = New ArrayList()
- ' Add elements of different types
- myList.Add(10) ' Integer
- myList.Add("Hello") ' String
- myList.Add(True) ' Boolean
- ' Display Count and Capacity
- Console.WriteLine("Initial Count: " & myList.Count)
- Console.WriteLine("Initial Capacity: " & myList.Capacity)
- ' Access and update an item using the Item property
- Console.WriteLine("Item at index 0: " & myList.Item(0))
- myList.Item(0) = 15 ' Update the first element
- Console.WriteLine("Updated item at index 0: " & myList.Item(0))
- ' Insert an element at a specific index
- myList.Insert(1, 20) ' Inserts 20 at index 1
- Console.WriteLine("Item at index 1 after insertion: " & myList.Item(1))
- ' Check if the ArrayList contains a specific value
- Console.WriteLine("Contains 15: " & myList.Contains(15))
- ' Find the index of a specific value
- Console.WriteLine("Index of 20: " & myList.IndexOf(20))
- ' Remove an element by value
- myList.Remove("Hello") ' Removes the first occurrence of "Hello"
- ' Remove an element by index
- If myList.Count > 2 Then ' Safeguard against out-of-range errors
- myList.RemoveAt(2) ' Removes the element at index 2
- End If
- ' Display the final list using a For Each loop
- Console.WriteLine("Elements in ArrayList:")
- For Each item In myList
- Console.WriteLine(item)
- Next
- ' Clear the ArrayList
- myList.Clear()
- Console.WriteLine("Count after Clear: " & myList.Count)
- ' Display Capacity after Clear
- Console.WriteLine("Capacity after Clear: " & myList.Capacity)
- End Sub
- End Module
Advertisement
Add Comment
Please, Sign In to add comment