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

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

问题描述

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



move 复制命令工作为了这?
我尝试了,但它有一个错误:不兼容的类型。



我应该坚持for循环吗?

解决方案

为了安全起见,在动态数组上使用复制函数,因为它处理管理内部类型。数组必须具有相同的类型,即以相同的表达式声明:

  var 
a,b:串;

或定义一个自定义数组类型:

  type 
TStringArray =字符串数组;
var
a:TStringArray;
//...and其他地方
var
b:TStringArray;

那么你可以做:

  a:=复制(b,低(b),长度(b)); //真的很干净,但不必要的
//...or
a:= Copy(b,0,MaxInt); //动态数组总是有零低限
//和复制副本最多计数项

你必须在静态数组上使用一个循环,混合数组类型(不是我建议这样做)。

如果你真的要使用移动,请记住检查零长度,因为 A [0] 构造可以提高范围检查错误(除了 SizeOf(A [0]),它由编译器魔术处理,从不实际执行)。

另外从不 code> A = A [0] 或 SizeOf(A)=长度(A)* SizeOf(A [0])因为这只适用于静态数组,如果您以后尝试将巨大的代码库重构为动态数组,它将会非常糟糕。


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

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

Should I stick to the for-loop?

解决方案

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;  

then you can do:

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  

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-Loop)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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