程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 語法基礎: 使用不同的數據類型標記數組

語法基礎: 使用不同的數據類型標記數組

編輯:Delphi

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. 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved