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

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

问题描述

如何在不使用迭代语句的情况下将一个数组追加到相同类型的另一个数组(对于,而循环使用 )在Delphi中?

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

推荐答案

有两个动态数组 arr1 arr2

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天全站免登陆