gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 15:25:23 24 4
gpt4 key购买 nike

我正在学习 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com