如何复制数组? [英] How to copy array?
问题描述
我在德尔福这样一个基本问题,我解决不了。
I have such a basic problem in DelpI can't solve it.
我的code:
注:DATAR是在下面的方法的地方,但通常它是一类var.Just为理念,这是当地的
Note:DataR is local in the methods below,but usually it's a class var.Just for the concept it's local.
class procedure TCelebrity.BeginRead(var input:Array of byte);
var DataR:Array of byte;
begin
VirtualFree(@DataRead,High(DataRead),MEM_RELEASE);
SetLength(DataR,Length(input));
Move(input,DataR,Length(input));
end;
这编译,但移动后()被执行DATAR =零。
This compiles,but after Move() is executed DataR = nil.
第二个尝试:
class procedure TCelebrity.BeginRead(var input:Array of byte);
var DataR:Array of byte;
begin
VirtualFree(@DataRead,High(DataRead),MEM_RELEASE);
SetLength(DataR,Length(input));
DataR := Copy(input,0,Length(input));
end;
这根本不all.Error编译在第三行(DATAR:=复制(输入....)说:不兼容的类型
This doesn't compile at all.Error at the third line(DataR := Copy(input....) saying "Incompatible types".
在哪里的问题?他们是字节的所有阵列!
Where's the problem? They are all Array of byte!
推荐答案
为什么不使用?
SetLength(DataR,Length(input));
for i:=Low(input) to High(input) do
DataR[i]:=input[i];
BTW:如果你想有数组传递为参数,应声明它们作为一个类型,例如:
BTW: if you want to have arrays passing as parameter, you should declare them as a type, eg:
type
TMyArray = array of byte;
和使用TMyArray作为参数类型。
and use TMyArray as parameters type.
编辑:我被告知我左右较低的值。在我原来的职位是为我:= 0,但我:=低(输入),更安全,更纯净
I was notified about i lower value. In my original post it was for i:=0, but i:=Low(input) is safer and more pure.
这篇关于如何复制数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!