Belongs to : Keyword


The Array keyword provides single and multi dimensional arrays (indexable sequences) of data.

Delphi has three basic array types :

1.Static arrays

These are defined with fixed, unchangeable sizes. They may be single or multidimensional – the latter being an array of arrays (of arrays etc). The size and range of such a multidimensional array is always given for the highest, leftmost array – the parent array.

The size of each dimension is determined in two ways, which may be freely mixed in a multidimensional array :

Index type Where Index is an integer type, normally Byte or Word. The range of this type defines the dimension range. For example, a Byte gives a 0..255 range.

Ordinal..Ordinal Alternatively, the range of each dimension may be given by direct ordinal values, such as 22..44.

2.Dynamic arrays

Dynamic arrays have no preallocated storage. When defined, only a pointer is created. Such arrays must have their length set before they can be used. For example :

SetLength(dynArray, 5);

sets the dynArray single dimension array size to 5 elements. This allocates storage.

All dynamic arrays starts at index = 0.

Individual subarrays of a multidimensional dynamic array may have different sized dimensions – they are, of course, separate arrays. After one such SetLength operation, elements of the set array may be referenced, even though the rest of the array is undefined.

3.Open arrays

Both static and dynamic arrays may be passed to subroutines as parameters. If the array parameter definition has no range (ie, a dynamic array type), then you must, paradoxically pass a static array as a parameter. Such an array is referred to as an Open array. Delphi passes the length as a hidden parameter to the subroutine.

An open array may also be defined with const value type. This is called a Variant open array – it is mostly used to allow a variable number of argument value to be passed to a subroutine.

In order to pass a Dynamic array by reference, the array and the subroutine definition of the array must be via an array type definition. See the code for an example.

Use Copy to copy one array to another. But be warned - it only copies the first, highest array dimension - the new array will still refer to elements of the subarrays.

Example code

Static Array

Dynamic Arrays

Open Array as parameter