我如何声明数组时,我不知道之前持续运行时间? [英] How do I declare an array when I don't know the length until run time?

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

问题描述

我本来是被定义为一个全局变量数组[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.

推荐答案

作为德尔福4,德尔福支持动态数组。可以在运行时修改它们的尺寸和它们将保留你存储在旧的大小等元素的数据。他们可以持有任何同质类型,包括记录和其他数组的元素。你可以声明动态数组一样的,你申报正常,静态数组,只是省略了数组边界:

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;

虽然静态数组允许你同时指定下限和上限,一个动态数组低指数始终为零。高指数是由函数,它总是返回比数组的长度小一给出。对于任何动态数组 X 高(X)=长度(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.

一个全局变量可以被任何code进行访问,包括当地的程序。

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

动态数组类型的全局变量将被初始化为一个的的数组。它的长度将是零和称那阵列将是-1。 的阵列上仍然会返回零。

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是特殊的;它们是由编译器,通过引用计数管理。正因为如此,string类型的新的动态数组元素初始化为空。但是,如果我用整数代替,也就不会有新的元素的值的保证。它们可以是零,但他们可能是别的,太,就像独立的局部变量的初始值。

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.

德尔福7帮助文件都非常好,有人告诉我。请阅读更多关于动态数组那里。你可以找到整个在过去10年中生产了近任何Delphi code例如,在你的Delphi安装提供,还有VCL和RTL源$ C ​​$ C的使用示范。

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天全站免登陆