Download presentation
Presentation is loading. Please wait.
1
Basic Collections
2
Collections Basics Collections are classes that contain a variable number of other objects Having methods to add and remove objects, to search for objects, to navigate the collection (iterate through the objects in the collection), etc. Collection Features Dynamic: whether they can increase the size, all collections in the System.Collections namespace are dynamic Indexed: whether they are indexed, ArrayList, BitArray, and SortedList classes are indexed Sorted: whether they are sorted, e.g. SortedList Access: how they are accessed, by index/key/position
3
Collection Interfaces
IEnumerable and IEnumerator (to navigate the collection) ICollection, IList, IDictionary Public Interface IEnumerable Function GetEnumerator() As IEnumerator End Interface Public Interface IEnumerator ReadOnly Property Current As Object ‘ returns current object Function MoveNext() As Boolean ‘ false if unsuccessful Sub Reset() ‘ reset position to 1st Public Interface ICollection ReadOnly Property Count As Integer Sub CopyTo(ByVal array As System.Array, ByVal index As Integer) ReadOnly Property IsSynchronized As Boolean ReadOnly Property SyncRoot As Object
4
ArrayList (1) Module Module1 Sub Main()
Dim a As ArrayList = New ArrayList a.Add("Joe") a.Add("Jane") a.Add("Jim") Dim i As Integer For i = 0 To a.Count - 1 Console.Out.WriteLine(a.Item(i)) Next Console.In.ReadLine() End Sub End Module
5
ArrayList (2) Module Module1 Sub Main()
Dim a As ArrayList = New ArrayList a.Add("Joe") a.Add("Jane") a.Add("Jim") Dim e As IEnumerator e = a.GetEnumerator() Dim s As String Do While e.MoveNext() s = CType(e.Current, String) Console.Out.WriteLine(s) Loop e.Reset() End Sub End Module
6
ArrayListExample Sub Main() Dim a As ArrayList = New ArrayList()
a.Add("Joe") a.Add("Jane") a.Add("Jim") Dim sObject As String = "theObject" a.Insert(2, sObject) If a.Contains(sObject) Then Console.Out.WriteLine("a contains sObject") Dim index As Integer index = a.IndexOf(sObject) Console.Out.WriteLine("the index of sObject in a is {0}", index) End If If a.IsReadOnly Then Console.Out.WriteLine("a is read-only") Else Console.Out.WriteLine("a is not read-only") a.RemoveAt(2) ' remove the sObject Console.Out.WriteLine("a STILL contains sObject") Console.Out.WriteLine("a does not contains sObject") End Sub
7
Hashtable A collection of key-value pairs Private Sub ht()
Dim ht As Hashtable = New Hashtable() Dim p1 As Person = New Person("Joe", #1/1/1961#) ht.Add(p1.name, p1) Dim p2 As Person = New Person("Jane", #2/2/1962#) ht.Add(p2.name, p2) Dim p3 As Person = New Person("Jim", #3/3/1963#) ht.Add(p3.name, p3) Dim p As Person If ht.Contains("Jim") Then p = CType(ht.Item("Jim"), Person) Console.Out.WriteLine("The age of {0} is {1}", p.name, p.getAge()) End If End Sub
8
More Collections SortedList: HashTableExample and SortedListExample
Queue & Stack: QueueStackExample Create your own Collections
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.