有没有办法将整个数组复制到另一个数组中?(除了使用 For 循环) [英] Is there way of copying the whole array into another array? (Other than using a For-loop)

查看:31
本文介绍了有没有办法将整个数组复制到另一个数组中?(除了使用 For 循环)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将整个数组复制到另一个数组中?除了使用 for 循环.

Is there way of copying the whole array into another array? Other than using a for-loop.

移动copy 命令对此有用吗?我确实尝试过,但它有一个错误:不兼容的类型".

Does the move or copy command work for this? I did try but it had an error: "Incompatible types".

我应该坚持使用 for 循环吗?

Should I stick to the for-loop?

推荐答案

为了安全起见,对动态数组使用 Copy 函数,因为它在内部处理托管类型.数组必须是相同的类型,即在相同的表达式中声明:

To be on the safe side, use the Copy function on dynamic arrays, as it handles the managed types internally. The arrays must be of the same type, i.e. declared in the same expression:

var  
    a, b: array of string;

或通过定义自定义数组类型:

or by defining a custom array type:

type   
    TStringArray = array of string;  
var 
    a: TStringArray;  
//...and somewhere else
var  
    b: TStringArray;  

那么你可以这样做:

a := Copy(b, Low(b), Length(b));  //really clean, but unnecessary 
//...or   
a := Copy(b, 0, MaxInt);  //dynamic arrays always have zero low bound 
                          //and Copy copies only "up to" Count items  

您必须在静态数组和混合数组类型时使用循环(不是我建议这样做).
如果您真的必须使用 Move,请记住检查零长度,因为 A[0] 构造可能会引发范围检查错误,(SizeOf(A[0]),由编译器魔法处理,从不实际执行.
另外从不假设 A = A[0]SizeOf(A) = Length(A) * SizeOf(A[0]),因为这仅适用于静态数组,如果您以后尝试将庞大的代码库重构为动态数组,它会非常困扰您.

You'll have to use a loop on static arrays and when mixing array types (not that I'd recommend doing it).
If you really have to use Move, remember checking for zero-length, as the A[0] constructs can raise range checking errors, (with the notable exception of SizeOf(A[0]), which is handled by compiler magic and never actually executes).
Also never assume that A = A[0] or SizeOf(A) = Length(A) * SizeOf(A[0]), as this is true only for static arrays and it will bite you really badly if you later try to refactor huge codebase to dynamic arrays.

这篇关于有没有办法将整个数组复制到另一个数组中?(除了使用 For 循环)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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