如何在Delphi中将一个数组附加到另​​一个相同类型的数组? [英] How to append one array to another array of same type in Delphi?

查看:17
本文介绍了如何在Delphi中将一个数组附加到另​​一个相同类型的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Delphi 中不使用迭代语句(forwhile 循环)将一个数组附加到另​​一个相同类型的数组?

How to append one array to another array of the same type without using iterative statements (for or while loops) in Delphi?

推荐答案

有两个 动态数组 arr1arr2

var
  arr1, arr2: array of Integer;
. . .
SetLength(arr1, 3);
arr1[0] := 1;
arr1[1] := 2;
arr1[2] := 3;

SetLength(arr2, 3);
arr2[0] := 4;
arr2[1] := 5;
arr2[2] := 6;

您可以像这样将第一个附加到第二个:

you can append the first to the second like this:

SetLength(arr2, Length(arr2) + Length(arr1));
Move(arr1[0], arr2[3], Length(arr1) * SizeOf(Integer));

请参阅 System.Move.

作为 Uwe Raabe 的评论 指出,您可以对托管类型执行以下操作:

As Uwe Raabe's comment points out, you can do as follows for managed types:

SetLength(arr2, Length(arr2) + Length(arr1));
for i := Low(arr1) to High(arr1) do
  arr2[3+i] := arr1[i];

这篇关于如何在Delphi中将一个数组附加到另​​一个相同类型的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆