在Delphi中声明可变长度数组

use*_*043 2 arrays delphi procedure declare delphi-xe2

我在Delphi中有一个程序,目前看起来像这样:

Procedure Time.TimeDB(algorithm: string; Encode, Decode: InputFunction; N, R: Int);
VAR
    i : LongInt;
    Errors : Array[N] of LongInt;
BEGIN

for i := 0 to N-1 do
  Errors[i] := 0;

END;
Run Code Online (Sandbox Code Playgroud)

尽管在过程定义中声明了N,但我已经给出了错误,即传递给错误定义的N是未声明的标识符.但是,在BEGIN-END部分中识别出N. 是什么导致了这个以及如何在VAR部分声明可变长度数组?

And*_*and 11

你写array of Int来声明动态数组IntS:

procedure Time.TimeDB(algorithm: string; Encode, Decode: InputFunction; N, R: Int);
var
  i: int;
  errors: array of Int;
begin

  SetLength(errors, N);
  for i := 0 to N - 1 do
    Errors[i] := 0;

end;
Run Code Online (Sandbox Code Playgroud)

还要注意,如果一个数组的N元素,然后将它们收录0,1,... N - 1.没有元素索引N.

(另外,你确定integer你写的时候不是故意Int吗?)


该构造array[M..N] of Int称为静态数组.在这种情况下,M并且N必须是常量,像array[0..15] of TColor.您还获得了array[TMyType] of TMySecondType索引将是类型的静态数组声明TMyType,如array[byte] of TColorarray[TFontStyle] of cardinal.