数组Array of Double 和 数组类型TDoubleDynArray是不同的。数组是值类型,数组类型是引用类型。如果参数传递数组,可以传递数组类型也可以传递const参数,可避免数组的拷贝。如下面测试代码。
数组类型A赋值数组类型B,A和B指向的数组地址相同,但A和B的指针不同。对数组的值的更改会反映到A和B上。但如果重新设置数组的长度,会导致重新分配内存,这时A和B就指向不同的地址空间,A和B的更改就不会影响到对方。
unit Unit5; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Types, Dialogs, StdCtrls, ExtCtrls, RzPanel; type TForm5 = class(TForm) btn1: TButton; edt1: TEdit; btn2: TButton; edt2: TEdit; procedure btn1Click(Sender: TObject); procedure btn2Click(Sender: TObject); private procedure ChangeArray(AArray: array of Double); procedure ChangeArrayType(AArrayType: TDoubleDynArray); public { Public declarations } end; var Form5: TForm5; implementation {$R *.dfm} { TForm5 } procedure TForm5.btn1Click(Sender: TObject); var LArray: array of Double; begin SetLength(LArray, 10); ZeroMemory(@LArray[0], 10 * 8); edt1.Text := FloatToStr(LArray[0]); //输出0 ChangeArray(LArray); edt2.Text := FloatToStr(LArray[0]); //输出0 end; procedure TForm5.btn2Click(Sender: TObject); var LArray: TDoubleDynArray; begin SetLength(LArray, 10); ZeroMemory(@LArray[0], 10 * 8); edt1.Text := FloatToStr(LArray[0]); //输出0 ChangeArrayType(LArray); edt2.Text := FloatToStr(LArray[0]); //输出99 end; procedure TForm5.ChangeArray(AArray: array of Double); begin AArray[0] := 99; end; procedure TForm5.ChangeArrayType(AArrayType: TDoubleDynArray); begin AArrayType[0] := 99; end; end.