常量数组复制到动态数组在Delphi

我有一个固定的常量数组

constAry1: array [1..10] of byte = (1,2,3,4,5,6,7,8,9,10);

和动态数组

dynAry1: array of byte;

什么是值复制从constAry1dynAry1最简单的方法是什么?

请问,如果你有数组(多维)的常量数组改变?

constArys: array [1..10] of array [1..10] of byte = . . . . .

--------------解决方案-------------

function CopyByteArray(const C: array of Byte): TByteDynArray;
begin
SetLength(Result, Length(C));
Move(C[Low(C)], Result[0], Length(C));
end;

procedure TFormMain.Button1Click(Sender: TObject);
const
C: array[1..10] of Byte = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
var
D: TByteDynArray;
I: Integer;
begin
D := CopyByteArray(C);
for I := Low(D) to High(D) do
OutputDebugString(PChar(Format('%d: %d', [I, D[I]])));
end;

procedure TFormMain.Button2Click(Sender: TObject);
const
C: array[1..10, 1..10] of Byte = (
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));

var
D: array of TByteDynArray;
I, J: Integer;
begin
SetLength(D, Length(C));
for I := 0 to Length(D) - 1 do
D[I] := CopyByteArray(C[Low(C) + I]);

for I := Low(D) to High(D) do
for J := Low(D[I]) to High(D[I]) do
OutputDebugString(PChar(Format('%d[%d]: %d', [I, J, D[I][J]])));
end;

SetLength(dynAry, Length(constAry1));
Move(constAry1[Low(constAry1)], dynAry[Low(dynAry)], SizeOf(constAry));

分类:德尔福 时间:2015-03-15 人气:3
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.538 (s). 10 q(s)