Delphi.net Basics
  Home  |  Delphi .net Home  |  FrameWork Collections
 .NET Framework
 Namespace References

 System
 System.Collections
 System.Drawing - download only
 System.Globalization
 System.IO

 Articles and Tutorials

 Overview of .NET
 Delphi and .NET
 Winform Applications
 ASP .Net Applications
 ASP Web Services
 Framework Collections
 Framework String Handling
 Framework Files and Folders


 Author links

 

 
 
 FrameWork Collections
Collections are like arrays
Actually, that does not do them justice. Collections are powerful .Net Framework mechanisms (classes) for holding sets of data being processed by your application. They not only save the programmer from re-inventing such mechanisms, but are guaranteed to be robust and reliable.
 
Here are the basic types of collection (click on the blue name to see the reference information) :
 
 ArrayList
   - Like an array, but elements can be inserted and deleted as well!
 
 BitArray
   - A single dimension array of Boolean values that can be inserted/delete
 
 SortedList
   - A sorted list of key-value data pairs - an implementation of IDictionary
 
 HashTable
   - Used to hold very large lists of key-value data pairs
 
 Queue
   - A store of objects retrieved on a first in first out (FIFO) basis
 
 Stack
   - A store of objects retrieved on a last in first out (LIFO) basis

There is more to Collections than the above, but these are the key classes. We'll cover each in term.
 

ArrayList
Arrays in Delphi for .Net come in a number of flavours :
 
1. Native Delphi Static arrays
 
These are defined like this :
 
 var
   MyArray : Array[1..3] of Byte;

They are static since you declare space for the whole array at variable declaration time. You are then stuck with that size. However, you can also choose your array indexing - here we have 1 to 3.
 
2. Native Delphi Dynamic arrays
 
These are defined like this :
 
 var
   MyArray : Array of Byte;
 begin
   SetLength(MyArray, 3);

They are dynamic since you declare space at run time (using the SetLength run time library procedure). You can also redeclare the size, thereby extending or truncating the array at any time. You are, however, stuck with 0 based indexing - the example array has elements 0 to 2.
 
3. The .Net System.Array type
 
These are defined like this :
 
 var
   MyArray : System.Array;
 begin
   MyArray := System.Array.CreateInstance(TypeOf(Byte), 3);

These are like Native dynamic arrays, but are objects rather than primitive data types. They can contain multiple dimensions, and these are contained with the single array object (primitive multiple dimension arrays are in essence arrays of arrays).
 
4. The .Net System.Collections.ArrayList collection
 
These are defined like this :
 
 var
   MyArray : System.Collections.ArrayList;
 begin
   MyArray := System.Collections.ArrayList.Create;

Here we get to the .Net Collection type of array. It allows you to define a single or multidimensional array of objects. Elements are always object types. Unlike the previous arrays, you can freely add, update and delete elements.
 
It also provides methods for traversing, sorting and searching the array list.
 
You use an ArrayList when you are dealing with volatile data - for example, lists of items a user an freely edit.
 

BitArray
This is a special type of array list. You can freely add, update and remove elements, but it is always only a single dimension. This allows it to be very efficiently managed internally.
 
Each element is a Boolean, where true equates to 1 and false equates to 0.
 
You can perform logical And, Or and XOr of one BitArray against another, and mass setting, resetting or inversion of all BitArray element values.
 
One particular benefit of BitArrays is that you can create one from an Integer - the bits of the integer become BitArray elements.
 

HashTable
Hash Tables have a long heritage, from the very early days of computing. They were of value when fast, keyed access was required to large quantities of data. Not database data, but in memory data. Provide a key, and the data for the key is returned.
 
They work by 'hashing' (converting) the key value into an index into the data storage area. This large bucket (or in reality, buckets) of memory has one slot per element, but the key value is never stored. Access is only via this hashing algorithm, which manimpulates key values in a way that spreads access to the bucket, and also guarantees that only one key value is ever associated with one element slot.
 
The .Net implementation uses this baisc approach, but in a sense, it is irrevelant how it does this since the internals are hidden in the class. All you need to know is that you should use such a collection as described.
 
Methods are provided to access data by key or value.
 

SortedList
A SortedList implements the IDictionary interface. It is used when a list of key-values is required in key sorted sequence. As with other collections, elements can be freely added, updated and removed. The sort sequence is dynamically maintained after each such operation.
 
Methods are provided to find and extract elements by key or value, and even get or set the value of a key-value pair by its relative position in the list.
 
An enumerator can be generated that allows element by element retrieval from the list.
 

Queue
Like HashTables, queues are long standing storage mechanisms, doing exactly what the name says. Elements are queued up, and when retrieved, come out in first-in-first-out sequence.
 
They are typically used to hold messages that arrive asynchronously but which must be handled in arrival sequence.
 
You can peek the next value due to be dequeued without actually removing it.
 

Stack
This is similar to a queue, except that data is retrieved in last in last out sequence. Data is 'pushed' onto the stack, and 'popped' off.
 
Stacks are used when nesting or recursion is involved - allowing the current level to be 'pushed' onto the stack, and 'popped' upon return - the stack reflects the nesting of processing.
 
 
'
'
'
Delphi Programming © Neil Moffatt All rights reserved.  |  Home Page