// 使用 System.Move 进行快速数组复制 array

 program ArrayCopyExample;


{$APPTYPE CONSOLE}


uses

  SysUtils;


procedure CopyArray(const Source: array of Integer; var Dest: array of Integer);

begin

  if Length(Dest) <> Length(Source) then

    raise Exception.Create('Destination array must be the same length as the source array.');


  // 使用 System.Move 进行快速数组复制

  System.Move(Source[0], Dest[0], Length(Source) * SizeOf(Integer));

end;


var

  SourceArray, DestArray: array of Integer;

  i: Integer;

begin

  // 初始化源数组

  SetLength(SourceArray, 5);

  for i := 0 to Length(SourceArray) - 1 do

    SourceArray[i] := i + 1;


  // 初始化目标数组

  SetLength(DestArray, Length(SourceArray));


  // 复制数组

  CopyArray(SourceArray, DestArray);


  // 输出结果

  for i := 0 to Length(DestArray) - 1 do

    WriteLn('DestArray[', i, '] = ', DestArray[i]);


  ReadLn;

end.

留言