delphi - 如何在 pascal 中不使用 for 循环同时设置多个数组字段?

标签 delphi delphi-6

我正在学习 Pascal,目前遇到了有关数组操作的问题。我遇到过一种设置数组的方法,我在其他语言中见过这种方法,但我不知道如何在 Pascal 中执行类似的操作。

变量的声明如下所示:

rotationBounds: array of array of integer;
setLength(rotationBounds, 5, 5);

我想做这样的事情:

 rotationBounds :=
         [
          [0, 0, 0, 0, 0],
          [0, 1, 1, 0, 0],
          [0, 0, 1, 0, 0],
          [0, 0, 1, 1, 0],
          [0, 0, 0, 0, 0],
         ]; 

基本上,我试图直接设置一个多维数组,而不是循环遍历它。

我的重点之一是让它看起来像图片一样,易于阅读和理解。

有没有办法可以实现这样的目标?

我使用 Borland Delphi 6 来编译该程序。

最佳答案

在 Delphi 6 中没有对动态数组初始化的内置支持。我将为此使用一对辅助函数:

type
  TIntegerArray = array of Integer;
  TIntegerMatrix = array of TIntegerArray;

function IntegerArray(const Values: array of Integer): TIntegerArray;
var
  i: Integer;
begin
  SetLength(Result, Length(Values));
  for i := 0 to high(Result) do
    Result[i] := Values[i];
end;

function IntegerMatrix(const Values: array of TIntegerArray): TIntegerMatrix;
var
  i: Integer;
begin
  SetLength(Result, Length(Values));
  for i := 0 to high(Result) do
    Result[i] := Values[i];
end;

然后这样调用它:

var
  rotationBounds: TIntegerMatrix;
....
rotationBounds := IntegerMatrix([
  IntegerArray([0, 0, 0, 0, 0]),
  IntegerArray([0, 1, 1, 0, 0]),
  IntegerArray([0, 0, 1, 0, 0]),
  IntegerArray([0, 0, 1, 1, 0]),
  IntegerArray([0, 0, 0, 0, 0]),
]);

关于delphi - 如何在 pascal 中不使用 for 循环同时设置多个数组字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16375584/

相关文章:

delphi - 初始化磁盘时拒绝访问

C# 和 Delphi 集成

delphi - 如何在 TDBCheckBox 后代中将 NULL 值显示为未选中状态?

delphi - 快速访问(排序的)TList

mysql - FDQuery (Delphi XE6) 上的 "type mismatch expecting AutoInc actual LongWord"

Delphi 2009 和 Firebird 2.1 = 完整的 Unicode?

delphi - {}和//的区别?

delphi - 当隐藏一列时,VirtualStringTree 列应调整大小

delphi - 如何完成通过函数的无类型参数传递的记录?

delphi - 如何中止 TWeBrowser 导航进度?