我有48个要放入数组的数字。目前,我可以这样做
Tarcher[i] := 20;
Tarcher[2] := 24;
Tarcher[3] := 29;
Tarcher[4] := 35;
如何将数字和较短的代码添加到数组中? 谢谢 格伦
答案 0 :(得分:3)
如果所需的值不适合公式,则可以考虑
const
Tarcher_init : array[1..48] of integer
= (20,24,29,35,5,6,7,8,9,61,1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,0,
1,2,3,4,5,6,7,84);
var
Tarcher : array[1..48] of integer;
i : integer;
begin
for i := low(Tarcher) to high(Tarcher) do
Tarcher[i] := Tarcher_init[i];
{OR}
move(Tarcher_init,Tarcher,sizeof(Tarcher));
end;
自然,move
会更快。
答案 1 :(得分:3)
您可以定义一个常数。如果您预先定义了数组的类型,则可以简单地分配它:
type
ArchArray = array[0..47] of Integer;
现在定义常量:
const
Arches: ArchArray =
( // 48 values here
1, 2, 3, 4, 11, 33 ....
... 33
);
然后您可以(不需要循环):
var
Tarcher: ArchArray;
begin
Tarcher := Arches;
答案 2 :(得分:0)
假设所有数组值遵循相同的公式从一个数组插槽递增到另一个数组插槽,那么一个简单的循环就足够了,例如:
var
Tarcher: array[1..48] of integer;
value, i: integer;
value := 20;
for i := 1 to 48 do
begin
Tarcher[i] := value;
inc(value, 3 + i);
end;
结果:
Tarcher[1] = 20
Tarcher[2] = 24
Tarcher[3] = 29
Tarcher[4] = 35
Tarcher[5] = 42
Tarcher[6] = 50
Tarcher[7] = 59
Tarcher[8] = 69
Tarcher[9] = 80
Tarcher[10] = 92
Tarcher[11] = 105
Tarcher[12] = 119
Tarcher[13] = 134
Tarcher[14] = 150
Tarcher[15] = 167
Tarcher[16] = 185
Tarcher[17] = 204
Tarcher[18] = 224
Tarcher[19] = 245
Tarcher[20] = 267
Tarcher[21] = 290
Tarcher[22] = 314
Tarcher[23] = 339
Tarcher[24] = 365
Tarcher[25] = 392
Tarcher[26] = 420
Tarcher[27] = 449
Tarcher[28] = 479
Tarcher[29] = 510
Tarcher[30] = 542
Tarcher[31] = 575
Tarcher[32] = 609
Tarcher[33] = 644
Tarcher[34] = 680
Tarcher[35] = 717
Tarcher[36] = 755
Tarcher[37] = 794
Tarcher[38] = 834
Tarcher[39] = 875
Tarcher[40] = 917
Tarcher[41] = 960
Tarcher[42] = 1004
Tarcher[43] = 1049
Tarcher[44] = 1095
Tarcher[45] = 1142
Tarcher[46] = 1190
Tarcher[47] = 1239
Tarcher[48] = 1289