B[0] := 2;
end;
the value of
A[0]
is 2. (If
A
and
B
were static arrays,
A[0]
would still be 1.)
Assigning to a dynamic-array index (for example,
MyFlexibleArray[2] := 7
) does not reallocate the array.
Out-of-range indexes are not reported at compile time.
In contrast, to make an independent
copy of a dynamic array, you must use the global
Copy
function:
var
A, B: array of Integer;
begin
SetLength(A, 1);
A[0] := 1;
B := Copy(A);
B[0] := 2; { B[0] <> A[0] }
end;
When dynamic-array variables are compared,
their references are compared, not their array values. Thus, after
execution of the code
var
A, B: array of Integer;
begin
SetLength(A, 1);
SetLength(B, 1);
A[0] := 2;
B[0] := 2;
end;
A = B
returns False but
A[0] = B[0]
returns True.
To truncate a dynamic array, pass it to
SetLength
, or pass it to
Copy
and assign the result back to the array variable.
(The
SetLength
procedure is usually faster.) For example, if
A
is
a dynamic array,
A := SetLength(A, 0,
20)
truncates all but the first 20 elements of
A
.
Once a dynamic array has been allocated, you can pass it to the standard functions
Length
,
High
, and
Low
.
Length
returns the number
of elements in the array,
High
returns the array's highest index (that is,
Length -
1
), and
Low
returns 0. In the case of a zero-length array,
High
returns 1 (with the anomalous consequence that
High < Low
).
Note:
In some function and procedure declarations, array parameters
are represented as
array of
baseType,
without any index types specified. For example,
function CheckStrings(A: array of string):
Boolean;
This indicates that the function operates on all arrays of the specified base type, regardless of their size, how they
are indexed, or whether they are allocated statically or dynamically.
Dostları ilə paylaş: