Arrays have both upper and lower bounds, and the elements of the array are contiguous within those bounds. Elements of the array are values that are all of the same type (string, integer, record, custom object).

In Delphi, there are two types of arrays: a fixed-size array which always remains the same size - static array, and a dynamic array whose size can change at run-time. Static Arrays Suppose we are writing a program that lets a user enter some values (e.g. the number of appointments) at the beginning of each day. We would choose to store the information in a list. We could call this list Appointments, and each number might be stored as Appointments[1], Appointments[2], an so on.

To use the list, we must first declare it. For example:

    var Appointments : array[0..6] of Integer;

declares a variable called Appointments that holds an one-dimensional array (vector) of 7 integer values. Given this declaration, Appointments[3] denotes the fourth integer value in Appointments. The number in the brackets is called the index.

If we create a static array but don't assign values to all its elements, the unused elements contain random data; they are like uninitialized variables. The following code can be used to set all elements in the Appointments array to 0.

    for k := 0 to 6 do Appointments[k] := 0;

Sometimes we need to keep track of related information in an array. For example, to keep track of each pixel on your computer screen, you need to refer to its X and Y coordinates. This can be done using a multidimensional array to store the values.

With Delphi, we can declare arrays of multiple dimensions. For example, the following statement declares a two-dimensional 7 by 24 array:

    var DayHour : array[1..7, 1..24] of Real;

To compute the number of elements in a multidimensional array, multiply the number of indexes. The DayHour variable, declared above, sets aside 168 (7*24) elements, in 7 rows and 24 columns. To retreive the value from cell in the third row and seventh column we would use: DayHour[3,7] or DayHour[3][7]. The following code can be used to set all elements in the DayHour array to 0.

    for i := 1 to 7 do
      for j := 1 to 24 do
        DayHour[i,j] := 0;

Note: here's How to Declare and Initialize Constant Arrays Dynamic Arrays Sometimes you may not know exactly how large to make an array. You may want to have the capability of changing the size of the array at run time. A dynamic array declares its type, but not its size. The actual size of a dynamic array can be changed at run time by the use of the SetLength procedure.

For example, the following variable declaration

var Students : array of string;

creates a one-dimensional dynamic array of strings. The declaration does not allocate memory for Students. To create the array in memory, we call SetLength procedure. For example, given the declaration above,

SetLength(Students, 14) ;

allocates an array of 14 strings, indexed 0 to 13. Dynamic arrays are always integer-indexed, always starting from 0 to one less than their size in elements.

To create a two-dimensional dynamic array, use the following code:

var Matrix: array of array of Double; begin SetLength(Matrix, 10, 20) end;

which allocates space for a two-dimensional, 10 x 20, array of Double floating-point values.

Note: To remove a dynamic array's memory space we assign nil to the array variable, like:

Matrix := nil;

Very often, your program doesn't know at compile time how many elements will be needed, that number will not be known until runtime. With dynamic arrays you can allocate only as much storage as is required at a given time. In other words, the size of dynamic arrays can be changed at run time, which is one of the key advantages to dynamic arrays. The next code creates an array of integer values and then calls the Copy function to resize the array.

    var
       Vector: array of Integer;
       k : integer;
    begin
       SetLength(Vector, 10) ;
       for k := Low(Vector) to High(Vector) do
         Vector[k] := i*10;
       ...
       //now we need more space
       SetLength(Vector, 20) ;
       //here, Vector array can hold up to 20 elements
       //(it already has 10 of them)
    end;

Note 1: SetLength function creates a larger (or smaller) array, and copies the existing values to the new array.

Note 2: The Low and High functions ensure you access every array element without looking back in your code for the correct lower and upper index values.

Arrays as Function Return Types

In Delphi, functions are routines that return a value.

When you want a function to return an array type variable, you might be tempted to use the next declaration:

    function GetWeekTotal(weekIndex : integer) : array[0..6] of integer;
    begin
      //this will NOT compile
    end;

When you try to compile this code, you'll get the next compile-time error: [Pascal Error] E2029 Identifier expected but 'ARRAY' found.

Obviously, when you declare functions that will return array value, you cannot include index type specifiers return declaration.

In order to allow a function to return an array value, you first need to create a custom array type, then use it as a return function type:

    //this WILL compile
    type
      TDayVisitors = array[0..6] of integer;
 
    ...
 
    function GetWeekTotal(weekIndex : integer) : TDayVisitors;
    begin
      //do some calculation for the provided "week"
    end;

Arrays as Method/Routine Properties Similar to using arrays as function return types, when you declare routines that take array parameters, you cannot include index type specifiers in the parameter declarations.

    type
      TDayVisitors = array[0..6] of integer;
 
    ...
 
    procedure DisplayWeekTotal(weekVisitors : TDayVisitors) ;
    begin
      //display some info for the provided "week"
    end;