delphi 不同类型数组_Delphi中的数组数据类型

本文介绍了 Delphi 编程中的两种数组类型:静态数组和动态数组。静态数组大小固定,需要预先声明,未初始化的元素含有随机数据。动态数组在声明时未指定大小,可在运行时通过 SetLength 函数调整大小,提供更大的灵活性。
摘要由CSDN通过智能技术生成

delphi 不同类型数组

Arrays allow us to refer to a series of variables by the same name and to use a number (an index) to call out individual elements in that series. Arrays have both upper and lower bounds and the elements of the array are contiguous within those bounds.

数组允许我们使用相同的名称引用一系列变量,并使用数字(索引)来调用该系列中的各个元素。 数组具有上限和下限,并且数组的元素在这些范围内是连续的。

Elements of the array are values that are all of the same type (string, integer, record, custom object).

数组的元素是所有具有相同类型的值(字符串,整数,记录,自定义对象)。

In Delphi, there are two types of arrays: a fixed-size array which always remains the same size--a static array--and a dynamic array whose size can change at runtime.

在Delphi中,有两种类型的数组:始终保持相同大小的固定大小的数组(静态数组)和动态数组,其大小可以在运行时改变。

静态数组 ( Static Arrays )

Suppose we are writing a program that lets a user enter some values (e.g. the number of appointments) at the beginning of each day. We would choose to store the information in a list. We could call this list Appointments, and each number might be stored as Appointments[1], Appointments[2], and so on.

假设我们正在编写一个程序,该程序可以让用户在每天开始时输入一些值(例如约会次数)。 我们将选择将信息存储在列表中。 我们可以将此列表称为Appointments ,每个数字都可以存储为Appointments [1],Appointments [2]等。

To use the list, we must first declare it. For example:

要使用列表,我们必须首先声明它。 例如:


var Appointments : array[0..6] of Integer; 

declares a variable called Appointments that holds a one-dimensional array (vector) of 7 integer values. Given this declaration, Appointments[3] denotes the fourth integer value in Appointments. The number in the brackets is called the index.

声明一个名为Appointments的变量,该变量包含7个整数值的一维数组(向量)。 给定此声明,Appointments [3]表示约会中的第四个整数值。 方括号中的数字称为索引。

If we create a static array but don’t assign values to all its elements, the unused elements contain random data; they are like uninitialized variables. The following code can be used to set all elements in the Appointments array to 0.

如果我们创建一个静态数组但不为其所有元素分配值,则未使用的元素将包含随机数据。 它们就像未初始化的变量。 以下代码可用于将Appointments数组中的所有元素设置为0。


for k := 0 to 6 do Appointments[k] := 0; 

Sometimes we need to keep track of related information in an array. For example, to keep track of each pixel on your computer screen, you need to refer to its X and Y coordinates using a multidimensional array to store the values.

有时我们需要跟踪数组中的相关信息。 例如,要跟踪计算机屏幕上的每个像素,您需要使用多维数组引用其X和Y坐标来存储值。

With Delphi, we can declare arrays of multiple dimensions. For example, the following statement declares a two-dimensional 7 by 24 array:

使用Delphi,我们可以声明多维数组。 例如,以下语句声明一个二维的7 x 24数组:


var DayHour : array[1..7, 1..24] of Real; 

To compute the number of elements in a multidimensional array, multiply the number of elements in each index. The DayHour variable, declared above, sets aside 168 (7*24) elements, in 7 rows and 24 columns. To retrieve the value from the cell in the third row and seventh column we would use: DayHour[3,7] or DayHour[3][7]. The following code can be used to set all elements in the DayHour array to 0.

要计算多维数组中的元素数,请乘以每个索引中的元素数。 上面声明的DayHour变量在7行24列中预留了168(7 * 24)个元素。 要从第三行和第七列的单元格中检索值,我们将使用:DayHour [3,7]或DayHour [3] [7]。 以下代码可用于将DayHour数组中的所有元素设置为0。


for i := 1 to 7 do
for j := 1 to 24 do
DayHour[i,j] := 0;

动态数组 ( Dynamic Arrays )

You may not know exactly how large to make an array. You may want to have the capability of changing the size of the array at runtime. A dynamic array declares its type, but not its size. The actual size of a dynamic array can be changed at runtime by the use of the SetLength procedure.

您可能不确切知道要制作一个数组的大小。 您可能需要具有在运行时更改数组大小的功能。 动态数组声明其类型,但不声明其大小。 动态数组的实际大小可以在运行时使用SetLength过程进行更改。


var Students : array of string; 

creates a one-dimensional dynamic array of strings. The declaration does not allocate memory for Students. To create the array in memory, we call SetLength procedure. For example, given the declaration above,

创建一维动态字符串数组。 该声明不为学生分配内存。 为了在内存中创建数组,我们调用SetLength过程。 例如,鉴于上述声明,


SetLength(Students, 14) ; 

allocates an array of 14 strings, indexed 0 to 13. Dynamic arrays are always integer-indexed, always starting from 0 to one less than their size in elements.

分配一个由14个字符串组成的数组,索引从0到13。动态数组始终是整数索引,总是从0到1小于其元素大小。

To create a two-dimensional dynamic array, use the following code:

要创建二维动态数组,请使用以下代码:


var Matrix: array of array of Double;
begin
SetLength(Matrix, 10, 20)
end;

which allocates space for a two-dimensional, 10-by-20 array of Double floating-point values.

它为Double浮点值的二维10×20数组分配空间。

To remove a dynamic array's memory space, assign nil to the array variable, like:

要删除动态数组的内存空间,请将nil分配给数组变量,例如:


Matrix := nil; 

Very often, your program doesn't know at compile time how many elements will be needed; that number will not be known until runtime. With dynamic arrays, you can allocate only as much storage as is required at a given time. In other words, the size of dynamic arrays can be changed at runtime, which is one of the key advantages of dynamic arrays.

很多时候,您的程序在编译时不知道需要多少个元素。 直到运行时才知道该数字。 使用动态阵列,您只能分配给定时间所需的存储量。 换句话说,动态数组的大小可以在运行时更改,这是动态数组的主要优势之一。

The next example creates an array of integer values and then calls the Copy function to resize the array.

下一个示例创建一个整数值数组,然后调用Copy函数以调整数组的大小。


var
Vector: array of Integer;
k : integer;
begin
SetLength(Vector, 10) ;
for k := Low(Vector) to High(Vector) do
Vector[k] := i*10;
...
//now we need more space
SetLength(Vector, 20) ;
//here, Vector array can hold up to 20 elements //(it already has 10 of them)end;

The SetLength function creates a larger (or smaller) array and copies the existing values to the new array. The Low and High functions ensure you access every array element without looking back in your code for the correct lower and upper index values.

SetLength函数创建一个更大(或更小)的数组,并将现有值复制到新数组 。 Low和High函数可确保您访问每个数组元素,而无需在代码中查找正确的上下索引值。

翻译自: https://www.thoughtco.com/using-array-data-types-in-delphi-1057644

delphi 不同类型数组

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值