To declare an array constant,
enclose the values of the array’s elements, separated by commas, in parentheses
at the end of the declaration. These values must be represented by constant
expressions. For example,
const
Digits: array[0..9] of Char = ('0', '1', '2', '3', '4', '5', '6',
'7', '8', '9');
declares a typed constant called Digits
that holds an array of characters.
Zero-based character arrays often
represent null-terminated strings, and for this reason string constants can be
used to initialize character arrays. So the declaration above can be more
conveniently represented as
const
Digits: array[0..9] of Char = '0123456789';
To define a multidimensional array
constant, enclose the values of each dimension in a separate set of
parentheses, separated by commas. For example,
type TCube
= array[0..1, 0..1, 0..1] of Integer;
const Maze:
TCube = (((0, 1), (2, 3)), ((4, 5), (6,7)));
creates an array called Maze where
Maze[0,0,0] = 0
Maze[0,0,1] = 1
Maze[0,1,0] = 2
Maze[0,1,1] = 3
Maze[1,0,0] = 4
Maze[1,0,1] = 5
Maze[1,1,0] = 6
Maze[1,1,1] = 7
Array constants cannot contain file-type values at any level.
Working with null-terminated strings
要声明数组常量,需要对常量的值进行封装:数组元素的值之间以逗号隔开,用圆括号将所有的元素括起来。数组元素的值都必需是常量表达式。例如,
const
Digits: array[0..9] of Char = ('0', '1', '2', '3', '4', '5', '6',
'7', '8', '9');
这里声明了一个叫做Digits的类型常量,它保存了一个字符数组。
零基字符数组通常表示空结束串,因此可以用串常量来初始化字符数组。因此上面的声明可以更便利地声明为:
const
Digits: array[0..9] of Char = '0123456789';
要定义一个多维数组常量,需要用圆括号封装每一维的值,并且每一维之间以逗号隔开。例如,
type TCube
= array[0..1, 0..1, 0..1] of Integer;
const Maze:
TCube = (((0, 1), (2, 3)), ((4, 5), (6,7)));
这里的声明创建了一个叫做Maze数组,各元素的值如下:
Maze[0,0,0] = 0
Maze[0,0,1] = 1
Maze[0,1,0] = 2
Maze[0,1,1] = 3
Maze[1,0,0] = 4
Maze[1,0,1] = 5
Maze[1,1,0] = 6
Maze[1,1,1] = 7
数组常量在任何层次都不能喊有文件类型的值。