Static array types are denoted by
constructions of the form
array[indexType1, ..., indexTypen]
of baseType
where each indexType is an ordinal
type whose range does not exceed 2GB. Since the indexTypes index the
array, the number of elements an array can hold is limited by the product of
the sizes of the indexTypes. In practice, indexTypes are usually
integer subranges.
In the simplest case of a
one-dimensional array, there is only a single indexType. For example,
var
MyArray: array[1..100] of Char;
declares a variable called MyArray
that holds an array of 100 character values. Given this declaration, MyArray[3]
denotes the third character in MyArray. If you create a static array but
don’t assign values to all its elements, the unused elements are still allocated
and contain random data; they are like uninitialized variables.
A multidimensional array is an
array of arrays. For example,
type
TMatrix = array[1..10] of array[1..50] of Real;
is equivalent to
type
TMatrix = array[1..10, 1..50] of Real;
Whichever way TMatrix is
declared, it represents an array of 500 real values. A variable MyMatrix
of type TMatrix can be indexed like this: MyMatrix[2,45]; or like this:
MyMatrix[2][45]. Similarly,
packed array[Boolean,1..10,TShoeSize]
of Integer;
is equivalent to
packed array[Boolean]
of packed array[1..10] of packed array[TShoeSize]
of Integer;
The standard functions Low
and High operate on array type identifiers and variables. They return
the low and high bounds of the array’s first index type. The standard function Length
returns the number of elements in the array’s first dimension.
A one-dimensional, packed, static
array of Char values is called a packed string. Packed-string types are
compatible with string types and with other packed-string types that have the
same number of elements. See Type
compatibility and identity.
An array type of the form array[0..x] of Char is called a zero-based character array. Zero-based character arrays are used to store null-terminated strings and are compatible with PChar values. See Working with null-terminated strings.
表示静态数组的结构形如
array[indexType1, ..., indexTypen]
of baseType
这里的每个索引类型indexType都是序数类型,它们的整个范围不能超过2GB。因为数组是由所有的索引类型indexTypes索引,所以静态数组中元素的数量是有限的,其大小是所有索引类型的尺寸的乘积。在实际应用中,索引类型通常是整数子界。
最简单的情况是一维数组,仅有一个单独的索引类型。例如,
var
MyArray: array[1..100] of Char;
这里声明了一个叫做MyArray的数组变量,用于保存100个字符值。对于此声明,MyArray[3]表示数组MyArray中第3个字符。如果创建一个静态数组而没有向其元素赋值,那么未使用的元素仍然被分配内存并且包含了随机数据;这与那些未初始化的变量是不同的。
多维数组就是元素是数组的数组。例如,
type
TMatrix = array[1..10] of array[1..50] of Real;
等价于
type
TMatrix = array[1..10, 1..50] of Real;
上面的两种声明,都代表一个用于存储500个实数值的数组。类型为TMatrix的变量MyMatrix可以被索引为:MyMatrix[2, 245] 或MyMatrix[2][45]。与上面的情况类似,
packed array[Boolean,1..10,TShoeSize]
of Integer;
等价于
packed array[Boolean]
of packed array[1..10] of packed array[TShoeSize]
of Integer;
标准函数Low和High可以作用于数组类型的标识符和数组变量。函数分别返回数组中第一个索引类型其范围的最低和最高值。标准函数Length返回数组中第一维的元素数量。
以Char类型为基类型的一维压缩静态数组叫做压缩串。压缩串类型与其他串类型、具有相同元素数量的其他压缩串是兼容的。见类型兼容和等同。
形如 array[0..x] of Char的数组类型叫做零基(zero-based)字符数组。零基字符数组用于存储空结束串,并与PChar类型的值兼容。见空结束串。