Array is a formal group of several objects of the same type considered as a whole. The type of array can be any of available system types. If an array type is not specified, the Variant type is set, an array can store various types of values.
A variable can be defined as an array directly during this variable description, without prior description of the array type.
Var
myArray: Array; // Array of the Variant type
Array size can be set during variable description (static array) or during initialization in application code (dynamic array). Arrays consisting of one dimension are named one-dimensional arrays. If on defining the number of elements in the array dimension one number is used (without ".." characters), the length of the array dimension is set from 0 to the specified value. Array dimensions are determined by the Cartesian product of elements in each array dimension.
Const // Range of arrays
Min = -10;
Max = 10;
Var
a: Array[Min..Max] Of Double; // One-dimensional array with explicit definition of array type
b: Array[Min..Max, Min..Max]; // Two-dimensional array without explicit definition of array type (by default - Variant)
c: Array[5, 5]; //Two-dimensional array with five elements in each dimension. Elements indexing in the range 0..4
Var
myArray: Array Of Integer; // Definition of data type in array
myArray1: Variant; // Definition of variable, which will contain array
Begin
//...
myArray := New Integer[10, 10]; // Create an array and determine its size
myArray1 := New Double[100]; // Create an array and determine its size
//...
To address array elements, use the structure: myArray[<index> {, <index>}]. The type specified value should match the array type.
Var
a: Array[5] Of Integer;
b: Array Of Double;
Begin
a[0] := 100; // Assign value to array element
b := New Double[5, 5];
b[0, 0] := Double.MinValue; // Assign value to array element
Values for elements can be specified both at the stage of array initialization and during code execution. To specify values on array initialization, specify the = character after the type, next in square brackets via a comma specify values according to the array type and size:
Var
arr1: Array;
arr2: Array[2, 2] Of Integer = [[11, 12], [21, 22]];
arr3: Array[3] Of String = ["A", 'B', #192]; // #192 - Cyrillic letter A
Begin
// Values of elements in arrays arr2 and arr3 before change
Debug.WriteLine("---Before changes---");
Debug.WriteLine(arr2[0, 0]);
Debug.WriteLine(arr3[2]);
// Change values of array elements
arr2[0, 0] := 101;
arr3[2] := "C";
arr1 := New Variant[2];
arr1[0] := arr2;
arr1[1] := arr3;
// Values of elements in arrays arr2 and arr3 after changing and saving to the arr1 array
Debug.WriteLine("---After changes---");
Debug.WriteLine((arr1[0] As Array Of Integer)[0, 0]);
Debug.WriteLine((arr1[1] As Array Of String)[2]);
An array that is equal to and compatible with another array in type can be specified as a value for the second array by means of assignment. In this case values will be copied from one array to another one.
The smaller array can be used as a value by one of the dimensions of the larger array. In this case one should make sure that size of the smaller array and size of the dimensions of the larger array must match. To set such a value, in the cell coordinate structure skip the element index by the dimension, which will be used for assignment. The rest of the cell coordinates determine the insertion area.
If the array is dynamic, and the dimension, by which the insertion is executed, is the last one, put comma before the skipped last index. If assignment is executed for the static array in the Var block, one does not need to put a comma before the last index.
Var
arrD1: Array[3] Of Double = [1, 2, 3];
arrD2: Array[3] Of Double;
arrD3: Array[3, 5] Of Double = [
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5]]; // The array has only two cell rows filled. The third row has zero values
arrD4: Array[5] Of Double = [6, 6, 6, 6, 6]; // Array that will be used as values of the third row of the arrD3 array
arrD5: Array[3] Of Double = [9, 9, 9]; // Array that will be used as values of the first column of the arrD3 array
Begin
// Set one array as a value of another array
arrD2 := arrD1;
arrD2[0] := 4;
Debug.WriteLine(arrD1[0]);
Debug.WriteLine(arrD2[0]);
// Set smaller array as a value by dimension of larger array
arrD3[2, ] := arrD4; // If dimension is the last in the list, do not put a comma
arrD3[, 0] := arrD5;
Therefore, elements of the arrD1 array will be copied to the arrD2 array, and the first element will be replaced. The array will look as follows: 4, 2, 3.
The arrD4 array will be inserted as a row value to the arrD3 array. In this case the arrD3 array that looked as follows:
[[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[0, 0, 0, 0, 0]]
will look as follows:
[[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[6, 6, 6, 6, 6]]
The arrD5 array will be inserted as a column value to the arrD3 array. In this case the arrD3 array will look as follows:
[[9, 2, 3, 4, 5],
[9, 2, 3, 4, 5],
[9, 6, 6, 6, 6]]
As any type of data, arrays can be created and stored in variables of the Variant type. To work with elements, an array should be obtained using the As operation, specifying the array type. The Array class also gives two methods to convert arrays of the Variant type to simple type arrays and back: Array.ToSimpleArray and Array.ToVariantArray.
Var
v: Variant;
d: Array Of Double;
Begin
v := New Double[3]; // Create a new array in variable of the Variant type
d := v As Array Of Double; // Access to array elements
d[0] := 1;
d[1] := 2;
d[2] := 3;
v := d; // Reassign to change source variable
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. Dimensions and types of arrays must match for correct copying.
The Variant data type can also be used to create a static array, which requires to save different data types. To do this, one should explicitly specify array element at the stage of its description:
Var
// Determine an array with the Variant type
data: array[7] Of Variant = [1, True, 't', 9223372036854775807, "pro", 1.123m, 1.123E5];
An array of the Variant type allows for storing the following data types: Boolean, Char, Decimal, Double, Integer, String, Int64.
See also: