本文转自万一老师的博客
原文出处: http://www.cnblogs.com/del/category/114641.html
静态数组的定义方法:
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//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;
-------------------------------------------------------------------------------
//给动态数组添加一个元素
type
TOneArray = array of Integer;
procedure AddArrayItem(Arr: TOneArray; x: Integer);
begin
SetLength(Arr,Length(Arr)+1);
Arr[High(Arr)] := x;
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 of Integer;
begin
{设置动态数组维数}
SetLength(arr,10);
{释放方法一}
arr := nil;
{释放方法二}
SetLength(arr, 0);
{释放方法三}
Finalize(arr);
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;
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
数组竟然可以这样定义:
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//这是常规思路:
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;