一,定義:

 

//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

 

 

    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()