Array Keyword
typeName
= array[Index type
] ofBase type
; // static array type typeName
= array[Index type
, ...] ofBase type
; // static array type typeName
= array ofBase type
; // dynamic array typeName
: array ofBase type
// open array as a subroutine parameterName
: array of const // open variant array as a subroutine parameter
Delphi has several different kinds of arrays: static arrays, dynamic arrays, and open arrays:
A static array is a traditional Pascal array. You can use any ordinal type as an index, and an array can have multiple indices. The size of a static array cannot change at runtime.
A dynamic
array is an array whose index type is Integer
and
whose size can change while the program runs. The lower bound of the
index is always zero, and the upper bound is set with the
SetLength
procedure. To copy a dynamic array, call
the Copy
procedure. Assigning a dynamic array
assigns a reference to the array without assigning the array’s
contents. Delphi uses reference counting to manage the lifetime of
dynamic arrays. Unlike strings, Delphi does not use copy-on-write for
dynamic arrays.
A
subroutine parameter can be an open array. You can pass any static or
dynamic array to the subroutine. Delphi passes an additional, hidden
parameter that gives the upper bound of the array. The subroutine
cannot change the size of a dynamic array that is passed as an open
array. Regardless of the index type of the actual array, the open
array parameter uses an Integer
index type, with
zero as the lower bound.
A special kind of open array is a variant open array, which is
declared as array
of
const
. Each element of the array is converted to a
TVarRec
record. The most common use for a variant
open array is to write a subroutine that takes a variable number of
arguments (such as the Format
function in the
SysUtils
unit).
An array of AnsiChar
, Char
, or
WideChar
is special when the index is an integer
range starting from zero. Delphi treats such an array as a string or
wide string (unless you disable the
$ExtendedSyntax
or $X
compiler
directives), except that you cannot pass a character array to a
subroutine that has a var
string parameter. You
can also pass an array reference as an argument to a subroutine that
takes a parameter of type PChar
or
PWideChar
. Delphi automatically passes the address
of the first character in the array.
Arrays are stored in column-major order, that is, the rightmost subscript varies fastest.
// Append a message to a log file. // See the example with AssignFile for the other overloaded // version of the Log procedure. procedure Log(const Fmt: string; const Args: array of const); overload; begin Log(Format(Fmt, Args)); end; // Append a random number to a dynamic array of integers. // Because dynamic arrays and open arrays use the same syntax, // you must use a named type for the dynamic array parameter. type TIntArray = array of integer; procedure AppendRandomInt(var Ints: TIntArray); begin SetLength(Ints, Length(Ints) + 1); Ints[High(Ints)] := Random(MaxInt); end; var Counter: Integer; TestInfo: string; Ints: TIntArray; I: Integer; begin ... Log('This is test #%d: %s', [Counter, TestInfo]); for I := 1 to 10 do AppendRandomInt(Ints); end.