chamsi09

Untitled

Dec 11th, 2024
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.93 KB | None | 0 0
  1. Imports System.Collections
  2. Module Program
  3.     Sub Main()
  4.         ' Create an ArrayList
  5.         Dim myList As ArrayList = New ArrayList()
  6.  
  7.         ' Add elements of different types
  8.         myList.Add(10)               ' Integer
  9.         myList.Add("Hello")          ' String
  10.         myList.Add(True)             ' Boolean
  11.  
  12.         ' Display Count and Capacity
  13.         Console.WriteLine("Initial Count: " & myList.Count)
  14.         Console.WriteLine("Initial Capacity: " & myList.Capacity)
  15.  
  16.         ' Access and update an item using the Item property
  17.         Console.WriteLine("Item at index 0: " & myList.Item(0))
  18.         myList.Item(0) = 15          ' Update the first element
  19.         Console.WriteLine("Updated item at index 0: " & myList.Item(0))
  20.  
  21.         ' Insert an element at a specific index
  22.         myList.Insert(1, 20)         ' Inserts 20 at index 1
  23.         Console.WriteLine("Item at index 1 after insertion: " & myList.Item(1))
  24.  
  25.         ' Check if the ArrayList contains a specific value
  26.         Console.WriteLine("Contains 15: " & myList.Contains(15))
  27.  
  28.         ' Find the index of a specific value
  29.         Console.WriteLine("Index of 20: " & myList.IndexOf(20))
  30.  
  31.         ' Remove an element by value
  32.         myList.Remove("Hello")       ' Removes the first occurrence of "Hello"
  33.  
  34.         ' Remove an element by index
  35.         If myList.Count > 2 Then     ' Safeguard against out-of-range errors
  36.             myList.RemoveAt(2)       ' Removes the element at index 2
  37.         End If
  38.  
  39.         ' Display the final list using a For Each loop
  40.         Console.WriteLine("Elements in ArrayList:")
  41.         For Each item In myList
  42.             Console.WriteLine(item)
  43.         Next
  44.  
  45.         ' Clear the ArrayList
  46.         myList.Clear()
  47.         Console.WriteLine("Count after Clear: " & myList.Count)
  48.  
  49.         ' Display Capacity after Clear
  50.         Console.WriteLine("Capacity after Clear: " & myList.Capacity)
  51.     End Sub
  52. End Module
  53.  
Advertisement
Add Comment
Please, Sign In to add comment