Delphi数组及长度
一,定义:
//1. 标准方法:
var
MyArr: array[0..10] of Integer; //定义静态一维数组
//2. 根据预定义类型来声明数组:
type
TMyArr = array[0..10] of Integer; //先定义一个数组类型
var
MyArr: TMyArr; //再定义静态数组
//3. 在非过程区可以直接赋值:
MyArr: array[0..2] of Integer = (11,22,33);
//4. 多维数组:
MyArr: array[0..2, 0..2] of Integer;
begin
MyArr[1,2] := 100;
end
二,求数组长度:
const
arr1: array[0..9] of Integer = (1,2,3,4,5,6,7,8,9,10);
arr2: array[0..1, 0..3] of Integer = ((1,2,3,4), (5,6,7,8));
var
arr3: array[Boolean] of Integer;
arr4: array[Byte] of Integer;
begin
ShowMessage(IntToStr(Length(arr1))); {10}
ShowMessage(IntToStr(SizeOf(arr1))); {40}
ShowMessage(IntToStr(Length(arr2))); {2}
ShowMessage(IntToStr(Length(arr2[0]))); {4}
ShowMessage(IntToStr(Length(arr2[1]))); {4}
ShowMessage(IntToStr(SizeOf(arr2))); {32}
ShowMessage(IntToStr(Length(arr3))); {2}
ShowMessage(IntToStr(SizeOf(arr3))); {8}
ShowMessage(IntToStr(Length(arr4))); {256}
ShowMessage(IntToStr(SizeOf(arr4))); {1024}
end;
总长度:SizeOf(arr) div SizeOf(Integer)
设置长度
var
StrArr: array of String; //动态数组定义时不与维数
begin
SetLength(StrArr,6); //分配6个元素位置: 0-5