Delphi比较二维数组是否一样

Delphi比较二维数组是否一样

Delphi二维数组,Delphi比较数组,Delphi比较二维数组

一句话: 用 CompareMem 对比内存即可.

{对比静态数组}

procedure TForm1.Button1Click(Sender: TObject);
var
arr1: array[0..3] of AnsiChar;
arr2: array[0..3] of Byte;
begin
arr1[0] := 'A'; arr1[1] := 'B'; arr1[2] := 'C'; arr1[3] := 'D';
arr2[0] := 65; arr2[1] := 66; arr2[2] := 67; arr2[3] := 68;

if CompareMem(@arr1, @arr2, SizeOf(arr1)) then
ShowMessage('arr1 与 arr2 中的数据相同');
end;
{对比动态数组}

procedure TForm1.Button2Click(Sender: TObject);
var
arr1: array of AnsiChar;
arr2: array of Byte;
begin
SetLength(arr1, 4);
SetLength(arr2, 4);
arr1[0] := 'A'; arr1[1] := 'B'; arr1[2] := 'C'; arr1[3] := 'D';
arr2[0] := 65; arr2[1] := 66; arr2[2] := 67; arr2[3] := 68;

if CompareMem(arr1, arr2, SizeOf(arr1)) then
ShowMessage('arr1 与 arr2 中的数据相同');
end;
{多维数组也一样}

procedure TForm1.Button3Click(Sender: TObject);
var
arr1: array[0..1, 0..1] of AnsiChar;
arr2: array[0..1, 0..1] of Byte;
begin
arr1[0,0] := 'A'; arr1[0,1] := 'B'; arr1[1,0] := 'C'; arr1[1,1] := 'D';
arr2[0,0] := 65; arr2[0,1] := 66; arr2[1,0] := 67; arr2[1,1] := 68;

if CompareMem(@arr1, @arr2, SizeOf(arr1)) then
ShowMessage('arr1 与 arr2 中的数据相同');
end;

你可能感兴趣的:(Delphi比较二维数组是否一样)