当我直到运行时才知道长度时,如何声明数组? [英] How do I declare an array when I don't know the length until run time?

查看:39
本文介绍了当我直到运行时才知道长度时,如何声明数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初有一个定义为全局变量的数组[1..1000].但是现在我需要将其设为 n,而不是 1000,而且我直到稍后才发现 n.我在填充数组之前知道 n 是什么,但我需要它是全局的,因此需要一种方法来在运行时定义全局数组的大小.

I originally had an array[1..1000] that was defined as a global variable. But now I need that to be n, not 1000 and I don't find out n until later. I know what n is before I fill the array up but I need it to be global therefore need a way to define the size of a global array at run time.

上下文正在使用文件中字节的线性变换填充数组.我不知道文件有多大,直到有人想打开它并且文件可以是任意大小.

Context is filling an array with a linear transformation of the bytes in a file. I don't know how big the file is until someone wants to open it and the files can be of any size.

推荐答案

从 Delphi 4 开始,Delphi 支持动态数组.您可以在运行时修改它们的大小,它们将以旧大小保留您存储在其他元素中的数据.它们可以保存任何同类类型的元素,包括记录和其他数组.您可以像声明普通的静态"数组一样声明动态数组,但只需省略数组边界:

As of Delphi 4, Delphi supports dynamic arrays. You can modify their sizes at run time and they will retain the data you stored in other elements at the old size. They can hold elements of any homogeneous type, including records and other arrays. You can declare a dynamic array the same as you declare normal, "static" arrays, but simply omit the array bounds:

var
  ArthurArray: array of TForm;

尽管静态数组允许您指定下限和上限,但动态数组的下限索引始终为零.高索引由 High 函数给出,它总是返回比数组长度小 1 的值.对于任何动态数组xHigh(x) = Length(x)-1.

Although static arrays allow you to specify both the lower and upper bound, the low index of a dynamic array is always zero. The high index is given by the High function, which always returns one less than the length of the array. For any dynamic array x, High(x) = Length(x)-1.

全局变量可以被任何代码访问,包括本地过程.

A global variable can be accessed by any code, including local procedures.

动态数组类型的全局变量将被初始化为一个数组.它的长度将为零,并且对该数组调用的 High 将为 -1.该数组上的 Low 仍将返回零.

A global variable of dynamic-array type will be initialized to be an empty array. Its length will be zero and High called on that array will be -1. Low on that array will still return zero.

您可以随时调整动态数组的大小.使用 SetLength 函数,就像处理字符串一样:

At any time, you may resize a dynamic array. Use the SetLength function, just as you can do with strings:

var
  NumElements: Integer;
begin
  NumElements := GetNumberOfArthurForms();
  SetLength(ArthurArray, NumElements);
end;

如果你有一个多维数组,你可以在一个循环中设置它们的长度:

If you have a multidimensional array, you can set their lengths in a loop:

var
  matrix: array of array of Double;
  i: Integer;
begin
  SetLength(matrix, height);
  for i := 0 to height - 1 do
    SetLength(matrix[i], width);
end;

有一个快捷方式可以一次设置所有内部数组的长度:

There's a shortcut for that to set the lengths of all the inner arrays at once:

begin
  SetLength(matrix, height, width);
end;

就像我提到的,动态数组在你调整它们的大小时会保持它们的旧值:

Like I mentioned, dynamic arrays keep their old values when you resize them:

var
  data: array of string;
begin
  SetLength(data, 2);
  data[1] := 'foo';
  SetLength(data, 20);
  Assert(data[1] = 'foo');
end;

但是如果你缩短数组,任何位于新的最后一个元素之外的元素都将永远消失:

But if you shorten the array, any elements that resided beyond the new last element are gone forever:

begin
  SetLength(data, 20);
  data[15] := 'foo';
  SetLength(data, 2);
  // data[15] does not exist anymore.
  SetLength(data, 16);
  writeln(data[15); // Should print an *empty* line.
end;

我上面的演示使用了字符串.字符串在 Delphi 中很特殊;它们由编译器通过引用计数进行管理.因此,字符串类型的新动态数组元素被初始化为空.但是如果我改用整数,就不能保证新元素的值.它们可能为零,但也可能是其他任何值,就像独立局部变量的初始值一样.

My demonstrations above used strings. Strings are special in Delphi; they're managed by the compiler through reference counts. Because of that, new dynamic-array elements of type string are initialized to be empty. But if I had used integers instead, there would be no guarantee of the values of new elements. They might be zero, but they might be anything else, too, just like the initial values of standalone local variables.

有人告诉我,Delphi 7 帮助文件非常好.请在那里阅读有关动态数组的更多信息.您可以在 Delphi 安装中提供的整个 VCL 和 RTL 源代码中以及在过去 10 年中生成的几乎所有 Delphi 代码示例中找到它们的使用演示.

The Delphi 7 help files are very good, I'm told. Please read more about dynamic arrays there. You can find demonstrations of their use throughout the VCL and RTL source code provided in your Delphi installation, as well as in nearly any Delphi code example produced in the last 10 years.

这篇关于当我直到运行时才知道长度时,如何声明数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆