요약 :2 차원 배열의 두 행을 교체하는 방법과 작동 원리는 무엇입니까?
아래의 David, Uwe 및 기타 전문가의 의견을 확인하십시오.
============================================== ==================================
다음 코드는 2 차원에서 두 행 스왑 double 값의 동적 배열. 나는 궁금해 : (1) 다음 코드는 2 차원 배열의 두 행을 교환하는 가장 좋은 방법입니다 여부? 그렇지 않다면, 이런 일을하는 가장 좋은 방법은 무엇입니까? (2) 왜 다음 코드가 작동합니까? 내 말은, 2 차원 배열이 아닌
연속
연속적인 메모리 섹션입니까? 다음 코드는 운에 의해서만 작동합니까? 어떤 제안이라도 감사드립니다!
unit Unit5;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TAADouble = array of array of Double;
TForm5 = class(TForm)
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form5: TForm5;
procedure SwapRows(arr: TAADouble; row0, row1: Integer);
implementation
{$R *.dfm}
procedure SwapRows(arr: TAADouble; row0, row1: Integer);
var
Tmp: Integer;
begin
{$IFDEF FPC}
Tmp := PtrUInt(arr[row0]);
PtrUInt(arr[row0]) := PtrUInt(arr[row1]);
PtrUInt(arr[row1]) := Tmp;
{$ELSE}
Tmp := Integer(arr[row0]);
Integer(arr[row0]) := Integer(arr[row1]);
Integer(arr[row1]) := Tmp;
{$ENDIF}
end;
procedure TForm5.FormShow(Sender: TObject);
var
tmpArray: TAADouble;
I, J: Integer;
rowStr: string;
begin
SetLength(tmpArray, 10, 10);
rowStr := '';
for I := 0 to 9 do
for J := 0 to 9 do
tmpArray[I][J] := I * J;
for I := 0 to 9 do
begin
rowStr := '';
for J := 0 to 9 do
rowStr := rowStr + FloatToStr(tmpArray[I][J]) + ' ';
OutputDebugString(PWideChar(rowStr));
end;
SwapRows(tmpArray, 3, 4);
for I := 0 to 9 do
begin
rowStr := '';
for J := 0 to 9 do
rowStr := rowStr + FloatToStr(tmpArray[I][J]) + ' ';
OutputDebugString(PWideChar(rowStr));
end;
end;
end.
을 -이 64 비트 버전이 도착하면 비참하게 실패 할 것입니다 ... 최근 Delphi를 사용하고 있다면 NativeInt 유형을 사용하십시오. 그렇지 않으면 직접 포인터 크기와 일치하도록 설정할 수 있습니다. –