Delphi - 声明矩阵常量

5

编辑我的问题。

我会具体说明。

如何将下面的代码声明为const而不是var? (我无法理解魔方示例)

 var
 Matrix : array of array of string;

 SetLength(Matrix, 8, 8);
 Matrix[0,0]:='A0';Matrix[0,1]:='A1';Matrix[0,2]:='A2';Matrix[0,3]:='A3';Matrix[0,4]:='A4';Matrix[0,5]:='A5';Matrix[0,6]:='A6';Matrix[0,7]:='A7';
 Matrix[1,0]:='B0';Matrix[1,1]:='B1';Matrix[1,2]:='B2';Matrix[1,3]:='B3';Matrix[1,4]:='B4';Matrix[1,5]:='B5';Matrix[1,6]:='B6';Matrix[1,7]:='B7';
 Matrix[2,0]:='C0';Matrix[2,1]:='C1';Matrix[2,2]:='C2';Matrix[2,3]:='C3';Matrix[2,4]:='C4';Matrix[2,5]:='C5';Matrix[2,6]:='C6';Matrix[2,7]:='C7';
 Matrix[3,0]:='D0';Matrix[3,1]:='D1';Matrix[3,2]:='D2';Matrix[3,3]:='D3';Matrix[3,4]:='D4';Matrix[3,5]:='D5';Matrix[3,6]:='D6';Matrix[3,7]:='D7';
 Matrix[4,0]:='E0';Matrix[4,1]:='E1';Matrix[4,2]:='E2';Matrix[4,3]:='E3';Matrix[4,4]:='E4';Matrix[4,5]:='E5';Matrix[4,6]:='E6';Matrix[4,7]:='E7';
 Matrix[5,0]:='F0';Matrix[5,1]:='F1';Matrix[5,2]:='F2';Matrix[5,3]:='F3';Matrix[5,4]:='F4';Matrix[5,5]:='F5';Matrix[5,6]:='F6';Matrix[5,7]:='F7';
 Matrix[6,0]:='G0';Matrix[6,1]:='G1';Matrix[6,2]:='G2';Matrix[6,3]:='G3';Matrix[6,4]:='G4';Matrix[6,5]:='G5';Matrix[6,6]:='G6';Matrix[6,7]:='G7';
 Matrix[7,0]:='H0';Matrix[7,1]:='H1';Matrix[7,2]:='H2';Matrix[7,3]:='H3';Matrix[7,4]:='H4';Matrix[7,5]:='H5';Matrix[7,6]:='H6';Matrix[7,7]:='H7';

1
你的问题是什么?这非常不清楚。 - David Heffernan
你的矩阵数组可以很容易地创建为一个常量 - 只需查看http://docwiki.embarcadero.com/RADStudio/Seattle/en/Declared_Constants#Array_Constants上的`Maze: TCube`示例。 - Arioch 'The
不,你不想创建某种矩阵。 - Free Consulting
为什么不呢?字符串是预定义的短语,我需要在运行时按下全局热键后访问它们。 - Flavio Correia
你的问题越来越不清楚了。 - Ken White
显示剩余6条评论
2个回答

8
您代码中的具体问题在于您声明的数组是动态的。也就是说,边界不是固定的,可以在运行时更改。
在旧版本的Delphi(XE6及更早版本)中,声明动态数组常量是不可能的。在XE7及更高版本中,虽然可以声明,但语法与固定数组常量不同。
在所有版本中,如果您声明了具有指定(因此是固定的)边界的常量数组,则可以指定常量数组的内容如下:
const
  Matrix : array[0..7, 0..7] of String = 
   (
    ('A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7'),
    ('B0', 'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7'),
    ('C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7'),
    ('D0', 'D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7'),
    ('E0', 'E1', 'E2', 'E3', 'E4', 'E5', 'E6', 'E7'),
    ('F0', 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7'),
    ('G0', 'G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7'),
    ('H0', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7')
   );

如果你在 Delphi XE6 之前的版本中需要动态数组,那么你不能使用如下声明初始化该数组。

如果你正在使用 Delphi XE7 或更高版本,则可以使用替代语法来声明动态数组常量。这与固定数组常量的语法非常相似,但使用方括号 [] 而不是普通括号 ()

 const
   Matrix : array of array of String =
   [
    ['A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7'],
    ['B0', 'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7'],
    ['C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7'],
    ['D0', 'D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7'],
    ['E0', 'E1', 'E2', 'E3', 'E4', 'E5', 'E6', 'E7'],
    ['F0', 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7'],
    ['G0', 'G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7'],
    ['H0', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7']
   ];

旧版 Delphi 的混合解决方案

如果您正在使用旧版 Delphi,则即使使用动态数组,如果您有一些初始状态(边界和内容)需要初始化,您也可以使用固定数组常量来定义该初始状态,然后在运行时从该常量初始化您的动态数组,例如:

const
  MX_DIM  = 8;
  MX_DEFAULT : array[0..MX_DIM - 1, 0..MX_DIM - 1] of String =
   (
    ('A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7'),
    ('B0', 'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7'),
    ('C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7'),
    ('D0', 'D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7'),
    ('E0', 'E1', 'E2', 'E3', 'E4', 'E5', 'E6', 'E7'),
    ('F0', 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7'),
    ('G0', 'G1', 'G2', 'G3', 'G4', 'G5', 'G6', 'G7'),
    ('H0', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7')
   );

// Then in your code:

var
  x, y: Integer;
  Matrix: array of array of String;
begin
  // Initialise 'Matrix' from MX_DEFAULT:

  SetLength(Matrix, MX_DIM, MX_DIM);
  for x := 0 to Pred(MX_DIM) do
    for y := 0 to Pred(MX_DIM) do
      Matrix[x, y] := MX_DEFAULT[x, y];
end;

1
实际上,在Delphi Seattle(或者是XE8?)以及更新版本中,您可以声明动态数组常量,就像声明字符串常量一样(毕竟字符串也是动态数组),使用[...]语法,而不是(...) - Rudy Velthuis
1
实际上是XE7 - David Heffernan
2
RIP Rudy Velthuis - user30478

6

文档展示了如何声明常量数组。

Array Constants

To declare an array constant, enclose the values of the elements of the array, 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.

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

以下示例适用于一维和三维数组。对于二维数组,代码如下:

const
  Coords: array [0..2, 0..2] of string = (
    ('A0', 'A1', 'A2'),
    ('B0', 'B1', 'B2'),
    ('C0', 'C1', 'C2'),
  );

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接