unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} //使用 Byte、Word、Boolean 标记数组 procedure TForm1.Button1Click(Sender: TObject); var arr1: array[Byte] of Integer; arr2: array[Word] of Integer; arr3: array[Boolean] of Integer; begin ShowMessage(IntToStr(Length(arr1))); {256} arr1[Low(arr1)] := 11; arr1[High(arr1)] := 22; ShowMessageFmt(‘%d, %d‘, [arr1[0], arr1[255]]); {11, 22} ShowMessage(IntToStr(Length(arr2))); {65536} arr2[0] := 33; arr2[65535] := 44; ShowMessageFmt(‘%d, %d‘, [arr2[Low(arr2)], arr2[High(arr2)]]); {33, 44} ShowMessage(IntToStr(Length(arr3))); {2} arr3[False] := 55; arr3[True] := 66; ShowMessageFmt(‘%d, %d‘, [arr3[Low(arr3)], arr3[High(arr3)]]); {55, 66} end; //使用 "子界" 标记数组 procedure TForm1.Button2Click(Sender: TObject); type TC = ‘a‘..‘z‘; var arr: array[TC] of Integer; n1,n2,n3: Integer; begin ShowMessage(IntToStr(Length(arr))); {26} arr[‘a‘] := 11; arr[‘b‘] := 22; arr[‘z‘] := 33; n1 := arr[Chr(97)]; n2 := arr[‘abc‘[2]]; n3 := arr[‘z‘]; ShowMessageFmt(‘%d,%d,%d‘, [n1,n2,n3]); {11,22,33} end; //使用 "枚举" 标记数组 procedure TForm1.Button3Click(Sender: TObject); type TMyEnum = (A, B, C, D); var arr: array[TMyEnum] of Integer; i: Integer; begin arr[A] := 11; arr[B] := 22; arr[C] := 33; arr[D] := 44; for i in arr do ShowMessage(IntToStr(i)); { 11/22/33/44 } end; //还是使用 "枚举" 标记数组 procedure TForm1.Button4Click(Sender: TObject); type TMyEnum = (X=1, Y=3, Z=5); var arr: array[TMyEnum] of Integer; i,n1,n2,n3: Integer; begin arr[X] := 11; arr[Y] := 22; arr[Z] := 33; i := 1; n1 := arr[TMyEnum(i)]; i := 3; n2 := arr[TMyEnum(i)]; i := 5; n3 := arr[TMyEnum(i)]; ShowMessageFmt(‘%d,%d,%d‘, [n1,n2,n3]); {11,22,33} end; end.
时间: 2024-10-12 22:38:45