问题来源: http://www.cnblogs.com/del/archive/2008/05/28/1208964.html#1211428
本例效果图:
本例效果图:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; Button2: TButton; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); begin Memo1.Align := alLeft; end; {多维动态数组} procedure TForm1.Button1Click(Sender: TObject); var Arr: array of array of array of Integer; {定义一个三维动态数组} i,j,k,ic,jc,kc: Integer; begin {设置数组} SetLength(Arr, 2,3,4); {获取各维的维数} ic := Length(Arr); jc := Length(Arr[0]); kc := Length(Arr[0][0]); {赋值} for i := 0 to ic - 1 do for j := 0 to jc - 1 do for k := 0 to kc - 1 do Arr[i][j][k] := Random(100); {取值} Memo1.Clear; for i := 0 to ic - 1 do for j := 0 to jc - 1 do for k := 0 to kc - 1 do Memo1.Lines.Add(Format('Arr[%d][%d][%d] = %d', [i, j, k, Arr[i][j][k]])); end; {多维动态数组指针} procedure TForm1.Button2Click(Sender: TObject); Type TMyArr = array of array of array of Integer; {定义一个三维动态数组类型} var PArr: ^TMyArr; {动态数组指针变量} i,j,k,ic,jc,kc: Integer; begin {用作维数的变量} ic := 2; jc := 3; kc := 4; {分配内存; 按说动态数组还需要 8 个管理字节, 我不知道 Delphi 是怎么协调的} GetMem(PArr, ic * jc * kc * SizeOf(Integer)); {设置数组} SetLength(PArr^, ic,jc,kc); {赋值} for i := 0 to ic - 1 do for j := 0 to jc - 1 do for k := 0 to kc - 1 do PArr^[i][j][k] := Random(100); {取值} Memo1.Clear; for i := 0 to ic - 1 do for j := 0 to jc - 1 do for k := 0 to kc - 1 do Memo1.Lines.Add(Format('Arr[%d][%d][%d] = %d', [i, j, k, PArr^[i][j][k]])); {自己分配的内存要负责释放} FreeMem(PArr); end; end.