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

发布于 2024-07-25 04:51:44 字数 369 浏览 3 评论 0原文

我有一个固定常量数组

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

和一个动态数组

dynAry1: array of byte;

将值从 constAry1 复制到 dynAry1 的最简单方法是什么?

如果你有一个常量数组数组(多维),它会改变吗?

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

I have a fixed constant array

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

and a dynamic array

dynAry1: array of byte;

What is the easiest way to copy the values from constAry1 to dynAry1?

Does it change if you have a const array of arrays (multidimensional)?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

还如梦归 2024-08-01 04:51:44

这会将 constAry1 复制到 dynAry

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

This will copy constAry1 to dynAry.

SetLength(dynAry, Length(constAry1));
Move(constAry1[Low(constAry1)], dynAry[Low(dynAry)], SizeOf(constAry1));
屌丝范 2024-08-01 04:51:44
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;
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;
掩于岁月 2024-08-01 04:51:44

从 Delphi XE7 开始,允许对数组使用类似字符串的操作。 然后就可以直接声明动态数组的常量了。 例如:

const
  KEY: TBytes = [$97, $45, $3b, $3e, $c8, $14, $c9, $e1];

From Delphi XE7, the use of string-like operations with arrays is allowed. Then you can declare a constant of a dynamic array directly. For example:

const
  KEY: TBytes = [$97, $45, $3b, $3e, $c8, $14, $c9, $e1];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文