在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
//1. 标准方法: var MyArr: array[0..10] of Integer; //定义静态数组 //2. 可以使用非0下标: var MyArr: array[9..10] of Integer; //不能提倡,这样不容易与系统函数沟通 //3. 根据预定义类型来声明数组: type TMyArr = array[0..10] of Integer; //先定义一个数组类型 var MyArr: TMyArr; //再定义静态数组 //4. 在非过程区可以直接赋值: var MyArr: array[0..2] of Integer = (11,22,33); //5. 多维数组: var MyArr: array[0..2, 0..2] of Integer; begin //使用 MyArr[1,2] := 100; end; //6. 根据子界定义数组: type TRange = 0..10; var MyArr: array[TRange] of Integer; //7. 根据枚举定义数组: type TEnums = (Enum1,Enum2,Enum3); var MyArr: array[TEnums] of string; begin MyArr[Enum1] := '万一'; ShowMessage(MyArr[Enum1]); //万一 end; //8. 根据其他类型定义数组: var MyArr: array[Byte] of Char; begin MyArr[255] := #65; ShowMessage(MyArr[255]); //A end; //应尽量不使用内建类型,可以新建类型: type TNewByte = Byte; var MyArr: array[TNewByte] of Char; begin MyArr[255] := #65; ShowMessage(MyArr[255]); //A end; //也可以使用类型别名: type TChar = type Char; var MyArr: array[TChar] of Byte; begin MyArr['C'] := 255; ShowMessage(IntToStr(MyArr['C'])); //255 end; //同时定义类型: type MyRec = record s: string; r: Real; b: Byte; end; var Arr1: array[0..100] of MyRec; Arr2: array[0..100] of record s: string; r: Real; b: Byte; end; //可以直接这样定义 Arr3: packed array[0..100] of MyRec; //压缩数组定义, 好像没有区别?
//例1: var StrArr: array of String; //动态数组定义时不与维数 begin SetLength(StrArr,6); //分配6个元素位置: 0-5 StrArr[0] := '万一'; //动态数组的下界是 0 ShowMessage(StrArr[0]); //分配空间后和静态数组一样使用 StrArr := nil; //一般没必要手动释放, 动态数组离开作用域会自释放 end; //例2. 动态数组的引用: var Arr1,Arr2: array of Integer; a: array[0..1] of Integer; begin SetLength(Arr1,6); Arr1[5] := 100; Arr2 := Arr1; //Arr2 引用了 Arr1 ShowMessage(IntToStr(Arr2[5])); //100 ShowMessage(IntToStr(Length(Arr2))); //当然 Arr2 维数也会是 6 ShowMessage(IntToStr(SizeOf(Arr1))); //4, 其实动态数组是个指针 ShowMessage(IntToStr(SizeOf(Arr2))); //4 Arr2[5] := 99; //现在它们指向同一个数组, 改变这个就是改变那个 ShowMessage(IntToStr(Arr1[5])); //99 Arr1 := nil; //释放其中一个指针, 数组继续存在 ShowMessage(IntToStr(Arr2[5])); //99 end; //例3. 数组 Copy <1>: var Arr1,Arr2: array of Integer; begin SetLength(Arr1,6); Arr1[5] := 100; Arr2 := Copy(Arr1); //数组 Copy Arr2[5] := 99; //改变 Arr2 不再影响 Arr1 ShowMessage(IntToStr(Arr1[5]-Arr2[5])); //1 SetLength(Arr1,7); ShowMessage(IntToStr(Length(Arr1))); //7 ShowMessage(IntToStr(Length(Arr2))); //6, 没有一点牵扯了 end; //例4. 数组 Copy <2>: var Arr1,Arr2: array of Integer; i: Integer; begin SetLength(Arr1,6); for i := Low(Arr1) to High(Arr1) do //给每个元素赋值 Arr1[i] := i+1; Arr2 := Copy(Arr1,1,3); //只 Copy 第2..4个元素 ShowMessage(IntToStr(Arr1[1])); //2, 现在 Arr2[0] 和 Arr1[1] 的值是一样的 ShowMessage(IntToStr(Arr2[0])); //2 ShowMessage(IntToStr(Length(Arr1))); //6, 维数肯定不一样了 ShowMessage(IntToStr(Length(Arr2))); //3 end; //例5. 动态多维数组: var Arr: array of array of Integer; //定义多维数组 begin SetLength(Arr,5,5); //分配空间 Arr[0,3] := 100; //赋值 ShowMessage(IntToStr(Arr[0,3])); //取值 end; //例6. 另类建立: var Arr: array of Integer; begin Arr := varArrayCreate([0,3],varInteger); ShowMessage(IntToStr(Length(Arr))); //4 Arr := VarArrayOf([1,2,3,4]); ShowMessage(IntToStr(Arr[0])); //1 {这是给变体数组使用的,可能会有效率问题} end; var Arr: array[0..3] of Variant; begin Arr[0] := 123; Arr[1] := 'wy'; Arr[2] := True; Arr[3] := VarArrayOf([1,'wanyi',1.5]); //变体数组的元素也可以是变体数组 ShowMessage(Arr[0]); //123, 不需要转换 ShowMessage(Arr[1]); //wy ShowMessage(Arr[2]); //True ShowMessage(Arr[3][1]); //wanyi end; type TMyEnum = (Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday); {定义枚举} var weekArr: array[TMyEnum] of string; {定义数组} myEnum: TMyEnum; {定义枚举变量} begin {数组赋值} weekArr[Monday] := '星期一'; weekArr[Tuesday] := '星期二'; weekArr[Wednesday] := '星期三'; weekArr[Thursday] := '星期四'; weekArr[Friday] := '星期五'; weekArr[Saturday] := '星期六'; weekArr[Sunday] := '星期天'; {调用数组} ShowMessage(weekArr[Sunday]); {星期天} {遍历数组} for myEnum := Low(weekArr) to High(weekArr) do begin ShowMessage(weekArr[myEnum]); {将分别显示: 星期一 ... 星期天} end; end;
type TABC = 'A'..'G'; {定义子界} var abcArr: array[TABC] of Integer; {定义数组} abc: TABC; {定义子界变量} begin {数组赋值} abcArr['A'] := 11; abcArr['B'] := 22; abcArr['C'] := 33; abcArr['D'] := 44; abcArr['E'] := 55; abcArr['F'] := 66; abcArr['G'] := 77; {调用数组} ShowMessage(IntToStr(abcArr['G'])); {77} {遍历数组} for abc := Low(abcArr) to High(abcArr) do begin ShowMessage(IntToStr(abcArr[abc])); {11 22 33 44 55 66 77} end; end; //动态数组一般是不需要手动释放的, 如果需要... var arr: array of Integer; begin {设置动态数组维数} SetLength(arr,10); {释放方法一} arr := nil; {释放方法二} SetLength(arr, 0); {释放方法三} Finalize(arr); end; //这是常规思路: const arr: array[0..1] of Char = ('A','B'); begin ShowMessage(arr); {AB} ShowMessage(IntToStr(Length(arr))); {2} end; //没想到可以这样: const arr: array[Boolean] of Char = ('A','B'); begin ShowMessage(arr); {AB} ShowMessage(IntToStr(Length(arr))); {2} {访问元素} ShowMessage(arr[False]); {A} ShowMessage(arr[True]); {B} {也可以这样访问} ShowMessage(arr[Low(arr)]); {A} ShowMessage(arr[High(arr)]); {B} {但不能这样访问} // ShowMessage(arr[0]); // ShowMessage(arr[1]); {但可以变通一下} ShowMessage(arr[Boolean(0)]); {A} ShowMessage(arr[Boolean(1)]); {B} end; //同理, 这样也可以: const arr: array[Byte] of Integer = ( 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29, 30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55, 56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81, 82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105, 106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125, 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145, 146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165, 166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185, 186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205, 206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225, 226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245, 246,247,248,249,250,251,252,253,254,255,256); begin ShowMessage(IntToStr(arr[Low(arr)])); {1} ShowMessage(IntToStr(arr[High(arr)])); {256} ShowMessage(IntToStr(Length(arr))); {256} end; //给这种用法来一个必要性的实例: const BoolStr: array[Boolean] of string = ('False', 'True'); var b: Boolean; begin b := True; ShowMessage(BoolStr[b]); {True} b := not b; ShowMessage(BoolStr[b]); {False} end; 我来提醒大家
数组定义时,如果唯数是以枚举开始的话,编译指令是从Low()..High() 所以带有负数值的枚举用来定义数组常量会出错. 如: Type TErrorCode = (ecNOERROR = 0, ecDisConnect = -1, ecOtherError = -2); Const ErrorLabels : array [TErrorCode] of String =( '无错误' , '断开连接' , '其他错误'); procedure TForm1.FormCreate(Sender: TObject); begin Caption := ErrorLabels[ecOtherError]; end; Caption得到的是:无错误,那是因为Low(TErrorCode)=ecOtherError High(TErrorCode)就是ecNOERROR //给一个整型开放数组求和的函数 function MyFun(const arr: array of Integer): Integer; var i: Integer; begin Result := 0; for i in arr do Result := Result + i; end; {测试1:} procedure TForm1.Button1Click(Sender: TObject); var num: Integer; begin num := MyFun([1,2,3]); ShowMessage(IntToStr(num)); {6} end; {测试2:} procedure TForm1.Button2Click(Sender: TObject); var iArr: array of Integer; i,x: Integer; begin SetLength(iArr, 10); for i := Low(iArr) to High(iArr) do begin iArr[i] := i + 1; end; x := MyFun(iArr); ShowMessage(IntToStr(x)); {55} end;
之前应该参考一下: 关于开放数组参数
//这是在 System 单元定义的一组标识数据类型的常量: vtInteger = 0; vtBoolean = 1; vtChar = 2; vtExtended = 3; vtString = 4; vtPointer = 5; vtPChar = 6; vtObject = 7; vtClass = 8; vtWideChar = 9; vtPWideChar = 10; vtAnsiString = 11; vtCurrency = 12; vtVariant = 13; vtInterface = 14; vtWideString = 15; vtInt64 = 16; //这是定义在 System 单元关于数据类型的一个结构: TVarRec = record case Byte of vtInteger: (VInteger: Integer; VType: Byte); vtBoolean: (VBoolean: Boolean); vtChar: (VChar: Char); vtExtended: (VExtended: PExtended); vtString: (VString: PShortString); vtPointer: (VPointer: Pointer); vtPChar: (VPChar: PChar); vtObject: (VObject: TObject); vtClass: (VClass: TClass); vtWideChar: (VWideChar: WideChar); vtPWideChar: (VPWideChar: PWideChar); vtAnsiString: (VAnsiString: Pointer); vtCurrency: (VCurrency: PCurrency); vtVariant: (VVariant: PVariant); vtInterface: (VInterface: Pointer); vtWideString: (VWideString: Pointer); vtInt64: (VInt64: PInt64); end; 作为参数的开放数组, 有时数组的成员类型是不确定的, 此时应该使用 array of const 定义; 详细举例: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} {把不同数据类型返回字符串的函数} function Fun1(arr: array of const): string; var i: Integer; begin Result := ''; for i := Low(arr) to High(arr) do begin case arr[i].VType of vtInteger : Result := Result + IntToStr(arr[i].VInteger) + ' '; vtBoolean : Result := Result + BoolToStr(arr[i].VBoolean, True) + ' '; vtChar : Result := Result + arr[i].VChar + ' '; vtExtended : Result := Result + FloatToStr(arr[i].VExtended^) + ' '; vtString : Result := Result + PShortString(arr[i].VString)^ + ' '; vtPointer : Result := Result + IntToStr(Integer(arr[i].VPointer)) + ' '; vtPChar : Result := Result + arr[i].VPChar + ' '; vtObject : Result := Result + arr[i].VObject.ClassName + ' '; vtClass : Result := Result + arr[i].VClass.ClassName + ' '; vtWideChar : Result := Result + arr[i].VWideChar + ' '; vtPWideChar : Result := Result + arr[i].VPWideChar + ' '; vtAnsiString: Result := Result + PAnsiChar(arr[i].VAnsiString)^ + ' '; vtCurrency : Result := Result + CurrToStr(arr[i].VCurrency^) + ' '; vtVariant : Result := Result + string(arr[i].VVariant^) + ' '; vtInterface : Result := Result + IntToStr(Integer(arr[i].VInterface)) + ' '; vtWideString: Result := Result + PWideChar(arr[i].VWideString) + ' '; vtInt64 : Result := Result + IntToStr(arr[i].VInt64^) + ' '; end; end; end; {简化上一个函数} function Fun2(const arr: array of const): string; 全部评论
专题导读
热门推荐
热门话题
阅读排行榜
|
请发表评论