Arrays 如何在整数/实数的二维动态数组上使用move?
在我的DelphiRio应用程序中,我使用了很多二维动态数组。为了加快一些操作,我想使用move命令而不是copy命令。我可以使它适用于一维动态数组,但对于二维或更高的数组,我不能。对于第二个维度,在发出move(A,B,size)之后,数组B的元素指向数组A元素的相同内存地址,即B引用A。事实上,我想分别使用A的B。请参阅我的代码:Arrays 如何在整数/实数的二维动态数组上使用move?,arrays,delphi,move,dynamic-arrays,Arrays,Delphi,Move,Dynamic Arrays,在我的DelphiRio应用程序中,我使用了很多二维动态数组。为了加快一些操作,我想使用move命令而不是copy命令。我可以使它适用于一维动态数组,但对于二维或更高的数组,我不能。对于第二个维度,在发出move(A,B,size)之后,数组B的元素指向数组A元素的相同内存地址,即B引用A。事实上,我想分别使用A的B。请参阅我的代码: program ProjArrayMove; {$APPTYPE CONSOLE} {$R *.res} uses Sy
program ProjArrayMove;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
Type
Arrayint = Tarray<Integer>;
Arrayreal = TArray<Real>;
MatrixInt = Array of Arrayreal;
var
vectorA, vectorB : Arrayint;
matA, matB : MatrixInt;
idx,idy : integer;
begin
TRY
// =============== TESTING WITH 1D DYNAMIC ARRAYS OF SIMPLE TYPES ==================
Writeln('==============================================================');
Writeln('========= TESTING 1-DIMENSION DYNAMIC ARAYS ==================');
Writeln('===============================================================');
readln;
Writeln('------- Fills Vector A ----------------------------');
SetLength(vectorA,5);
for idx :=0 to 4 do
begin
vectorA[idx] := (idx+1) * 10;
Writeln('VectorA : [' + idx.tostring + '] ' +
Format('Address : %p [%d]' ,[PPointer(@VectorA), VectorA[idx] ]) );
end;
readln;
Writeln('--------------------------------------------------');
Writeln('------ Moves VectorA to VectorB ------------------');
SetLength(VectorB,Length(vectorA));
Move(VectorA[0],VectorB[0],SizeoF(VectorA[0]) * Length(VectorA));
for idx :=0 to 4 do
Writeln('VectorB : [' + idx.tostring + '] ' +
Format('Address : %p [%d]' ,[PPointer(@VectorB), VectorB[idx] ]) );
readln;
Writeln('---------------------------------------------------');
Writeln('------ Changes VectorB contents ------------------');
for idx :=0 to 4 do
begin
vectorB[idx] := (idx+1) * 200;
Writeln('VectorB : [' + idx.tostring + '] ' +
Format('Address : %p [%d]' ,[PPointer(@VectorB), VectorB[idx] ]) );
end;
readln;
Writeln('--------------------------------------------------');
Writeln('------ Checking Vector A ------------------------');
for idx :=0 to 4 do
Writeln('VectorA : [' + idx.tostring + '] ' +
Format('Address : %p [%d]' ,[PPointer(@VectorA), VectorA[idx] ]) );
Writeln;
Writeln('CONCLUSION : ===>> MOVE command works fine for 1-Dimension Dynamic Arrays!');
readln;
//=========================== TESTING WITH MATRIX 2D DYNAMIC ARRAYS OF SIMPLE TYPES ==================
Writeln('===============================================================');
Writeln('========= TESTING 2-DIMENSIONS DYNAMIC ARAYS ==================');
Writeln('===============================================================');
readln;
Writeln('------ Fills MatrixA -----------------------------');
SetLength(matA,5,2);
for idx :=0 to 4 do
for idy := 0 to 1 do
begin
matA[idx][idy] := (idx +1) * (idy +1);
Writeln('Mat A : [' + idx.tostring + '][' + idy.tostring +'] ' +
Format('Address : %p [%f]' ,[PPointer(@matA[idx][idy]), matA[idx][idy] ] ));
end;
readln;
Writeln('-------------------------------------------------');
Writeln('------ Move MatrixA to MatrixB ------------------');
SetLength(matB,length(matA));
//move(matA[0],MatB[0],Sizeof(matA[0]) * length(matA));
move(matA,MatB,Sizeof(matA[0]) * length(matA));
for idx :=0 to 4 do
begin
Setlength(MatB[idx],length(matA[idx]));
//Move(matA[idx][0],matB[idx][0],sizeof(matB[idx][0]) * length(matB[idx]) );
Move(matA[idx],matB[idx],sizeof(matB[idx][0]) * length(matB[idx]) );
for idy := 0 to 1 do
begin
Writeln('Mat B : [' + idx.tostring + '][' + idy.tostring +'] ' +
Format('Address : %p [%f]' ,[PPointer(@matB[idx][idy]), matB[idx][idy] ] ));
end;
end;
readln;
Writeln('-------------------------------------------------');
Writeln('------ Change MatrixB content ------------------');
readln;
for idx :=0 to 4 do
for idy := 0 to 1 do
begin
matB[idx][idy] := 100.5 * (idx+1) * (idy +1);
Writeln('Mat B : [' + idx.tostring + '][' + idy.tostring +'] ' +
Format('Address : %p [%f]' ,[PPointer(@matB[idx][idy]), matB[idx][idy] ] ));
end;
Writeln('-------------------------------------------------');
Writeln('------ Checking Matrix A ------------------------');
readln;
for idx :=0 to 4 do
for idy := 0 to 1 do
Writeln('Mat A : [' + idx.tostring + '][' + idy.tostring +'] ' +
Format('Address : %p [%f]' ,[PPointer(@matA[idx][idy]), matA[idx][idy] ] ));
Writeln;
Writeln('CONCLUSION : ===>> MOVE command DOES NOT WORK on 2-Dimensions Dynamic Arrays!');
readln;
except
on E: Exception do
begin
Writeln(E.ClassName, ': ', E.Message);
readln;
end;
end;
end.
ProjArrayMove程序;
{$APPTYPE控制台}
{$R*.res}
使用
System.SysUtils;
类型
Arrayint=焦油;
Arrayreal=焦油;
MatrixInt=数组的数组;
变量
向量a,向量b:Arrayint;
matA,matB:MatrixInt;
idx,idy:整数;
开始
尝试
//================使用简单类型的1D动态数组进行测试==================
书面文件('================================================================================================================');
Writeln('======================================================================');
书面文件('===================================================================================================================');
readln;
Writeln('----填充向量A--------------');
设定长度(向量,5);
对于idx:=0到4 do
开始
向量a[idx]:=(idx+1)*10;
Writeln('VectorA:['+idx.tostring+']+
格式('Address:%p[%d],[PPointer(@VectorA),VectorA[idx]]);
结束;
readln;
书面语(“-------------------------------------------------------------”);
Writeln('----将VectorA移动到VectorB--------------------);
设定长度(向量B,长度(向量A));
Move(VectorA[0],VectorB[0],SizeoF(VectorA[0])*Length(VectorA));
对于idx:=0到4 do
Writeln('VectorB:['+idx.tostring+']+
格式('地址:%p[%d],[PPointer(@VectorB),VectorB[idx]]);
readln;
书面语(“------------------------------------------------------”);
Writeln('----更改向量内容--------------------);
对于idx:=0到4 do
开始
向量b[idx]:=(idx+1)*200;
Writeln('VectorB:['+idx.tostring+']+
格式('地址:%p[%d],[PPointer(@VectorB),VectorB[idx]]);
结束;
readln;
书面语(“-------------------------------------------------------------”);
Writeln('----检查向量A--------------');
对于idx:=0到4 do
Writeln('VectorA:['+idx.tostring+']+
格式('Address:%p[%d],[PPointer(@VectorA),VectorA[idx]]);
书面语;
Writeln('CONCLUSION:==>>MOVE命令适用于一维动态数组!');
readln;
//=====================================使用简单类型的矩阵2D动态数组进行测试==================
书面文件('===================================================================================================================');
Writeln('===========================================================================');
书面文件('===================================================================================================================');
readln;
Writeln('----填写矩阵--------------------');
设定长度(matA,5,2);
对于idx:=0到4 do
对于idy:=0到1 do
开始
马塔[idx][idy]:=(idx+1)*(idy+1);
Writeln('Mat A:['+idx.tostring+']['+idy.tostring+']+
格式('Address:%p[%f],[PPointer(@matA[idx][idy]),matA[idx][idy]]);
结束;
readln;
书面语(“-------------------------------------------------------------”);
Writeln('----将MatrixA移动到MatrixB--------------------);
设定长度(matB,长度(matA));
//移动(matA[0],MatB[0],Sizeof(matA[0])*长度(matA));
移动(matA、MatB、Sizeof(matA[0])*长度(matA));
对于idx:=0到4 do
开始
Setlength(MatB[idx],length(matA[idx]);
//移动(matA[idx][0]、matB[idx][0]、sizeof(matB[idx][0])*长度(matB[idx]);
移动(matA[idx]、matB[idx]、sizeof(matB[idx][0])*长度(matB[idx]);
对于idy:=0到1 do
开始
Writeln('Mat B:['+idx.tostring+']['+idy.tostring+']+
格式('Address:%p[%f],[PPointer(@matB[idx][idy]),matB[idx][idy]]);
结束;
结束;
readln;
书面语(“-------------------------------------------------------------”);
Writeln('----更改矩阵B内容--------------------);
readln;
对于idx:=0到4 do
对于idy:=0到1 do
开始
matB[idx][idy]:=100.5*(idx+1)*(idy+1);
Writeln('Mat B:['+idx.tostring+']['+idy.tostring+']+
格式('Address:%p[%f],[PPointer(@matB[idx][idy]),matB[idx][idy]]);
结束;
书面语(“-------------------------------------------------------------”);
Writeln('----检查矩阵A--------------');
readln;
对于idx:=0到4 do
对于idy:=0到1 do
Writeln('Mat A:['+idx.tostring+']['+idy.tostring+']+
格式('地址:%p[%f],[p]
Move(matA, matB, Sizeof(matA[0]) * Length(matA));
Move(matA[0], matB[0], Sizeof(matA[0]) * Length(matA));
function CloneMatrixInt(const matrix: array of ArrayInt): MatrixInt;
var
i: Integer;
begin
SetLength(Result, Length(matrix));
for i := 0 to High(Result) do
Result[i] := Copy(matrix[i]);
end;
type
ArrayMatrixInt = array of MatrixInt;
function CloneArrayMatrixInt(const arrayMatrix: array of MatrixInt): ArrayMatrixInt;
var
i: Integer;
begin
SetLength(Result, Length(arrayMatrix));
for i := 0 to High(Result) do
Result[i] := CloneMatrixInt(matrix[i]);
end;