Arrays Description

Syntax

$ ArraySection = IdentList ":" ARRAY ["[" DemNum"]"] [OF Type] ";"

$ DemNum = Integer[.. Integer ] {"," Integer [.. Integer ] }

$ type = String | Integer | Variant | Double | Decimal | Boolean | Char | DateTime

Description

An array is a formal group of several objects of the same type considered as a whole. The type of the array can be any of available system types.

A variable can be defined as an array directly during describing this variable, without prior description of the array type.

The number of elements in one array dimension is defined by the value of the DemNum parameter. If during defining the number of elements in the array dimension one number is used (without «..» symbols), the length of the array dimension is set from 0 to the specified value.

If the DemNum parameter is not specified, the array is considered dynamic. If the type of the array is not specified, the Variant type is set.

To call the array elements, a structure of the following type is used:

ArrIdent[ Integer {"," Integer} ] := Value;

Example

Creating static arrays:

Sub Main; //Declare unit start

Const // Describe unit constants

Min = -10;

Max = 10;

Var //Describe unit variables

a: Array[Min..Max] Of Double; //Describe a one-dimensional array a with explicit declaration of the array type

b: Array[Min..Max, Min..Max]; //Describe a two-dimensional array b without explicit declaration of the array type

//(by default - Variant)

i: Integer;

Begin //Program text start

For i := a.GetLowerBound(1) To a.GetUpperBound(1) Do

a[i] := Math.RandBetweenI(0, 100); // Element-by-element array filling

Debug.WriteLine("Element with index" + i.ToString + ": " + a[i].ToString);

End For;

End Sub Main;

Creating dynamic arrays:

Sub Main;

Var

DinArray: Array Of Integer; //type of data in array is defined

Begin

DinArray := New Integer[10, 10]; //an array is created, its dimensions are defined

//set of operators

End Sub Main;

In the example a two-dimensional dynamic array is created. Each array dimension contains 10 integer values. The elements of each dimension are indexed from 0 to 9.

As any type of data, arrays can be created and stored in the variables of the Variant type. To work with elements, an array has to be obtained using the As operation, specifying the array type.

Sub Main;

Var

v: Variant;

d: Array Of Double;

Begin

v := New Double[3]; //Creating a new array in the variable of the type Variant

d := v As Array Of Double; //Access to array elements

d[0] := 1;

d[1] := 2;

d[2] := 3;

v := d; //Reassigning to change the initial variable

End Sub Main;

NOTE. If a dynamic array is overridden in the program body, all the data existing before the recreation is lost.

When arrays are assigned, data is copied. For correct copying the size and type of the arrays must match.

See also:

The Array Data Type | Array Class | Descriptions and Syntax Rules