.Net Runtime Library for Delphi
Close
Non-generic Collections

Collections are data structures that holds data in different ways for flexible operations. Non-generic collections store items as Object. The runtime library Collection interfaces are defined as part of the CNClrLib.Collections namespace. The following are list of some of the non-generic collection types. with code examples.

ArrayList Collection

An ArrayList is a dynamic array. It provides random access to its elements. An ArrayList automatically expands as data is added. Unlike arrays, an ArrayList can hold data of multiple data types. Elements in the ArrayList are accessed via an integer index. Indexes are zero based. Indexing of elements and insertion and deletion at the end of the ArrayList takes constant time. Inserting or deleting an element in the middle of the dynamic array is more costly. It takes linear time. The example below demonstrates how to create, add, remove and access elements of an arraylist:

Delphi
program ArrayListExample; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, CNClrLib.Host, CNClrLib.Collections, CNClrLib.Core; var Console: _Console; ArrayList: _ArrayList; I: Integer; begin Console := CoConsole.CreateInstance; //Create new instance ArrayList ArrayList := CoArrayList.CreateInstance; //Add elements to the ArrayList ArrayList.Add('Pascal'); ArrayList.Add(344); ArrayList.Add(55); ArrayList.Add(CoClrObject.CreateInstance); //Remove one element from the ArrayList ArrayList.Remove(55); //Iterate through the arraylist and print its elements to the console for I := 0 to ArrayList.Count - 1 do Console.WriteLine_12(ArrayList[I]); end. //Output //Pascal //344 //System.Object









































 

Hashtable Collection

The Hashtable represents a collection of key/value pairs that are organized based on the hash code of the key. The following example shows how to create, initialize and perform various functions to a Hashtable and how to print out its keys and values.

Delphi
program HashtableExample; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, CNClrLib.Host, CNClrLib.Collections, CNClrLib.Core; var Console: _Console; openWith: _Hashtable; Values: _IEnumerator; Keys: _IEnumerator; I: Integer; s: string; begin Console := CoConsole.CreateInstance; //Create a new hash table. // openWith := CoHashtable.CreateInstance; // Add some elements to the hash table. There are no // duplicate keys, but some of the values are duplicates. openWith.Add('txt', 'notepad.exe'); openWith.Add('bmp', 'paint.exe'); openWith.Add('dib', 'paint.exe'); openWith.Add('rtf', 'wordpad.exe'); // The Add method throws an exception if the new key is // already in the hash table. try openWith.Add('txt', 'winword.exe'); except Console.WriteLine_14('An element with Key = ''txt'' already exists.'); end; // The Item property is the default property, so you // can omit its name when accessing elements. Console.WriteLine_15('For key = ''rtf'', value = {0}.', openWith['rtf']); // The default Item property can be used to change the value // associated with a key. openWith['rtf'] := 'winword.exe'; Console.WriteLine_15('For key = ''rtf'', value = {0}.', openWith['rtf']); // If a key does not exist, setting the default Item property // for that key adds a new key/value pair. openWith['doc'] := 'winword.exe'; // ContainsKey can be used to test keys before inserting // them. if not openWith.ContainsKey('ht') then begin openWith.Add('ht', 'hypertrm.exe'); Console.WriteLine_15('Value added for key = ''ht'': {0}', openWith['ht']); end; // Use while to enumerate hash table elements; // Use the Keys property (ICollection) inherited class called IEnumerable in the // form of a method in the ICollection interface called AsIEnumerable and get the // GetEnumerator for the keys. Enumerate through the key values and display both the // key and value of the hashtable per iteration. Console.WriteLine(); Keys := openWith.Keys.AsIEnumerable.GetEnumerator; while Keys.MoveNext do Console.WriteLine_17('Key = {0}, Value = {1}', Keys.Current, openWith.Item[Keys.Current]); // To get the values alone, use the Values property. Values := openWith.Values.AsIEnumerable.GetEnumerator; // The elements of the ValueCollection are strongly typed // with the type that was specified for hash table values. Console.WriteLine(); while Values.MoveNext do Console.WriteLine_15('Value = {0}', Values.Current); // Reset Keys Collection Enumerator values to point to the first data in the list // because it has been used above. Keys.Reset; // The elements of the KeyCollection are strongly typed // with the type that was specified for hash table keys. Console.WriteLine(); while Keys.MoveNext do Console.WriteLine_15('Key = {0}', Keys.Current); // Use the Remove method to remove a key/value pair. Console.WriteLine; Console.WriteLine_14('Remove(''doc'')'); openWith.Remove('doc'); if not openWith.ContainsKey('doc') then Console.WriteLine_14('Key ''doc'' is not found.'); end. (* This code example produces the following output: An element with Key = 'txt' already exists. For key = 'rtf', value = wordpad.exe. For key = 'rtf', value = winword.exe. Value added for key = 'ht': hypertrm.exe Key = rtf, Value = winword.exe Key = bmp, Value = paint.exe Key = dib, Value = paint.exe Key = doc, Value = winword.exe Key = txt, Value = notepad.exe Key = ht, Value = hypertrm.exe Value = winword.exe Value = paint.exe Value = paint.exe Value = winword.exe Value = notepad.exe Value = hypertrm.exe Key = rtf Key = bmp Key = dib Key = doc Key = txt Key = ht Remove('doc') Key 'doc' is not found. *)






















































































































 

Queue Collection

The Queue represents a first-in, first-out collection of objects. The queue is implemented as a circular array. Objects stored in a Queue are inserted at one end and removed from the other. Queues are useful when you need temporary storage for information; that is, when you might want to discard an element after retrieving its value. Use Queue if you need to access the information in the same order that it is stored in the collection. Queue accepts null as a valid value and allows duplicate elements. The following example shows how to create and add values to a Queue and how to print out its values.

Delphi
program QueueExample; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, CNClrLib.Host, CNClrLib.Collections, CNClrLib.Core; var Console: _Console; myQ: _Queue; myCollection: _IEnumerator; begin Console := CoConsole.CreateInstance; // Creates and initializes a new Queue. myQ := CoQueue.CreateInstance; myQ.Enqueue('Hello'); myQ.Enqueue('World'); myQ.Enqueue('!'); // Displays the properties and values of the Queue. Console.WriteLine_14('myQ'); Console.WriteLine_15(' Count: {0}', myQ.Count ); Console.Write_22(' Values:'); myCollection:= myQ.AsIEnumerable.GetEnumerator; while myCollection.MoveNext do Console.Write(' {0}', myCollection.Current); end. (* This code produces the following output. myQ Count: 3 Values: Hello World ! *)

































 

Stack Collection

The stack represents a simple last-in-first-out (LIFO) non-generic collection of objects. Stacks are useful when you need temporary storage for information; that is, when you might want to discard an element after retrieving its value. Use Stack if you need to access the information in reverse order. Stack accepts null as a valid value and allows duplicate elements. The following example shows how to create and add values to a Stack and how to display its values.

Delphi
program StackExample; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, CNClrLib.Host, CNClrLib.Collections, CNClrLib.Core; var Console: _Console; myStack: _Stack; myCollection: _IEnumerator; begin Console := CoConsole.CreateInstance; // Creates and initializes a new Stack. myStack := CoStack.CreateInstance; myStack.Push('Hello'); myStack.Push('World'); myStack.Push('!'); // Displays the properties and values of the Stack. Console.WriteLine_14('myStack'); Console.WriteLine_15(' Count: {0}', myStack.Count ); Console.Write_22(' Values:'); myCollection:= myStack.AsIEnumerable.GetEnumerator; while myCollection.MoveNext do Console.Write(' {0}', myCollection.Current); end. (* This code produces the following output. myStack Count: 3 Values: Hello World ! *)

































 

SortedList Collection

The SortedList represents a collection of key/value pairs that are sorted by the keys and are accessible by key and by index. A SortedList element can be accessed by its key, like an element in any IDictionary implementation, or by its index, like an element in any IList implementation.. The following code example shows how to create and initialize a SortedList object and how to print out its keys and values.

Delphi
program SortedListExample; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, CNClrLib.Host, CNClrLib.Collections, CNClrLib.Core; var Console: _Console; mySL: _SortedList; I: Integer; begin Console := CoConsole.CreateInstance; // Creates and initializes a new SortedList. mySL := CoSortedList.CreateInstance; mySL.Add('Third', '!'); mySL.Add('Second', 'World'); mySL.Add('First', 'Hello'); // Displays the properties and values of the SortedList. Console.WriteLine_14('mySL'); Console.WriteLine_15(' Count: {0}', mySL.Count ); Console.WriteLine_15(' Capacity: {0}', mySL.Capacity ); Console.WriteLine_14(' Keys and Values:'); Console.WriteLine_14(' -KEY- -VALUE-'); for I := 0 to mySL.Count - 1 do Console.WriteLine_17(' {0}: {1}', mySL.GetKey(I), mySL.GetByIndex(I) ); end. (* This code produces the following output. mySL Count: 3 Capacity: 16 Keys and Values: -KEY- -VALUE- First: Hello Second: World Third: ! *)









































 

BitArray Collections

The BitArray manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on (1) and false indicates the bit is off (0). The BitArray is a collection in which the capacity is always the same as the count. Elements are added to a BitArray by increasing the Length property; elements are deleted by decreasing the Length property. The following code example shows how to create and initialize a BitArray and how to print out its values.

Delphi
program BitArrayExample; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, CNClrLib.Host, CNClrLib.Collections, CNClrLib.Core; var Console: _Console; myBA1: _BitArray; myBA2: _BitArray; myBA3: _BitArray; myBA4: _BitArray; myBA5: _BitArray; myBytes: _ByteArray; myBools: _BooleanArray; myInts: _Int32Array; I: Integer; procedure PrintValues(myList: _IEnumerator; myWidth: Integer); var i: Integer; begin i := myWidth; while myList.MoveNext do begin if i <= 0 then begin i := myWidth; Console.WriteLine; end; Dec(i); Console.Write('{0,8}', myList.Current); end; Console.WriteLine; end; begin Console := CoConsole.CreateInstance; // Creates and initializes several BitArrays. myBA1 := CoBitArray.CreateInstance(5); myBA2 := CoBitArray.CreateInstance(5, false); myBytes := TClrArrayHelper.ToByteArray([1, 2, 3, 4, 5]); myBA3 := CoBitArray.CreateInstance(myBytes); myBools := TClrArrayHelper.ToBooleanArray([true, false, true, true, false]); myBA4 := CoBitArray.CreateInstance(myBools); myInts := TClrArrayHelper.ToInt32Array([ 6, 7, 8, 9, 10]); myBA5 := CoBitArray.CreateInstance(myInts); // Displays the properties and values of the BitArrays. Console.WriteLine_14('myBA1'); Console.WriteLine_15(' Count: {0}', myBA1.Count ); Console.WriteLine_15(' Length: {0}', myBA1.Length ); Console.WriteLine_14(' Values:' ); PrintValues(myBA1.GetEnumerator, 8); Console.WriteLine_14('myBA2'); Console.WriteLine_15(' Count: {0}', myBA2.Count ); Console.WriteLine_15(' Length: {0}', myBA2.Length ); Console.WriteLine_14(' Values:' ); PrintValues(myBA2.GetEnumerator, 8); Console.WriteLine_14('myBA3'); Console.WriteLine_15(' Count: {0}', myBA3.Count ); Console.WriteLine_15(' Length: {0}', myBA3.Length ); Console.WriteLine_14(' Values:' ); PrintValues(myBA3.GetEnumerator, 8); Console.WriteLine_14('myBA4'); Console.WriteLine_15(' Count: {0}', myBA4.Count ); Console.WriteLine_15(' Length: {0}', myBA4.Length ); Console.WriteLine_14(' Values:' ); PrintValues(myBA4.GetEnumerator, 8); Console.WriteLine_14('myBA5'); Console.WriteLine_15(' Count: {0}', myBA5.Count ); Console.WriteLine_15(' Length: {0}', myBA5.Length ); Console.WriteLine_14(' Values:' ); PrintValues(myBA5.GetEnumerator, 8); end. (* This code produces the following output. myBA1 Count: 5 Length: 5 Values: False False False False False myBA2 Count: 5 Length: 5 Values: False False False False False myBA3 Count: 40 Length: 40 Values: True False False False False False False False False True False False False False False False True True False False False False False False False False True False False False False False True False True False False False False False myBA4 Count: 5 Length: 5 Values: True False True True False myBA5 Count: 160 Length: 160 Values: False True True False False False False False False False False False False False False False False False False False False False False False False False False False False False False False True True True False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False False True False False False False False False False False False False False False False False False False False False False False False False False False False False False False True False False True False False False False False False False False False False False False False False False False False False False False False False False False False False False False False True False True False False False False False False False False False False False False False False False False False False False False False False False False False False False False *)








































































































































 

Comparer Interface

Compares two objects for equivalence, where string comparisons are case-sensitive. The following code example shows how Compare returns different values depending on the culture associated with the Comparer.

Delphi
program CompareExample; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, CNClrLib.Host, CNClrLib.Collections, CNClrLib.Core; var Console: _Console; ComparerStatic: _ComparerHelper; myCompIntl: _Comparer; CultureInfo1: _CultureInfo; myCompTrad: _Comparer; CultureInfoTrad: _CultureInfo; str1, str2: string; I: Integer; begin Console := CoConsole.CreateInstance; // Creates the strings to compare. str1 := 'llegar'; str2 := 'lugar'; Console.WriteLine_17('Comparing ''{0}'' and ''{1}'' ...', str1, str2); // Uses the DefaultInvariant Comparer. ComparerStatic := CoComparerHelper.CreateInstance; Console.WriteLine_15(' Invariant Comparer: {0}', ComparerStatic.DefaultInvariant.Compare(str1, str2)); // Uses the Comparer based on the culture "es-ES" (Spanish - Spain, international sort). CultureInfo1 := CoCultureInfo.CreateInstance('es-ES', false); myCompIntl := CoComparer.CreateInstance(CultureInfo1); Console.WriteLine_15(' International Sort: {0}', myCompIntl.Compare(str1, str2)); // Uses the Comparer based on the culture identifier 0x040A (Spanish - Spain, traditional sort). CultureInfoTrad := CoCultureInfo.CreateInstance($040A, false); myCompTrad := CoComparer.CreateInstance(CultureInfoTrad); Console.WriteLine_15(' Traditional Sort : {0}', myCompTrad.Compare(str1, str2)); end. (* This code produces the following output. Comparing 'llegar' and 'lugar' ... Invariant Comparer: -1 International Sort: -1 Traditional Sort : 1 *)