delphi 動(dòng)態(tài)數(shù)組的使用
來(lái)源:教育資源網(wǎng)
?
發(fā)布時(shí)間:2021-05-13 14:56:54
?
點(diǎn)擊:36
動(dòng)態(tài)數(shù)組的使用
//例1:
var
StrArr: array of String; //動(dòng)態(tài)數(shù)組定義時(shí)不與維數(shù)
begin
SetLength(StrArr,6); //分配6個(gè)元素位置: 0-5
StrArr[0] := '萬(wàn)一'; //動(dòng)態(tài)數(shù)組的下界是 0
ShowMessage(StrArr[0]); //分配空間后和靜態(tài)數(shù)組一樣使用
StrArr := nil; //一般沒必要手動(dòng)釋放, 動(dòng)態(tài)數(shù)組離開作用域會(huì)自釋放
end;
--------------------------------------------------------------------------------
//例2. 動(dòng)態(tài)數(shù)組的引用:
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))); //當(dāng)然 Arr2 維數(shù)也會(huì)是 6
ShowMessage(IntToStr(SizeOf(Arr1))); //4, 其實(shí)動(dòng)態(tài)數(shù)組是個(gè)指針
ShowMessage(IntToStr(SizeOf(Arr2))); //4
Arr2[5] := 99; //現(xiàn)在它們指向同一個(gè)數(shù)組, 改變這個(gè)就是改變那個(gè)
ShowMessage(IntToStr(Arr1[5])); //99
Arr1 := nil; //釋放其中一個(gè)指針, 數(shù)組繼續(xù)存在
ShowMessage(IntToStr(Arr2[5])); //99
end;
--------------------------------------------------------------------------------
//例3. 數(shù)組 Copy <1>:
var
Arr1,Arr2: array of Integer;
begin
SetLength(Arr1,6);
Arr1[5] := 100;
Arr2 := Copy(Arr1); //數(shù)組 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, 沒有一點(diǎn)牽扯了
end;
--------------------------------------------------------------------------------
//例4. 數(shù)組 Copy <2>:
var
Arr1,Arr2: array of Integer;
i: Integer;
begin
SetLength(Arr1,6);
for i := Low(Arr1) to High(Arr1) do //給每個(gè)元素賦值
Arr1[i] := i+1;
Arr2 := Copy(Arr1,1,3); //只 Copy 第2..4個(gè)元素
ShowMessage(IntToStr(Arr1[1])); //2, 現(xiàn)在 Arr2[0] 和 Arr1[1] 的值是一樣的
ShowMessage(IntToStr(Arr2[0])); //2
ShowMessage(IntToStr(Length(Arr1))); //6, 維數(shù)肯定不一樣了
ShowMessage(IntToStr(Length(Arr2))); //3
end;
--------------------------------------------------------------------------------
//例5. 動(dòng)態(tài)多維數(shù)組:
var
Arr: array of array of Integer; //定義多維數(shù)組
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
{這是給變體數(shù)組使用的,可能會(huì)有效率問題}
end;
//例1:
var
StrArr: array of String; //動(dòng)態(tài)數(shù)組定義時(shí)不與維數(shù)
begin
SetLength(StrArr,6); //分配6個(gè)元素位置: 0-5
StrArr[0] := '萬(wàn)一'; //動(dòng)態(tài)數(shù)組的下界是 0
ShowMessage(StrArr[0]); //分配空間后和靜態(tài)數(shù)組一樣使用
StrArr := nil; //一般沒必要手動(dòng)釋放, 動(dòng)態(tài)數(shù)組離開作用域會(huì)自釋放
end;
--------------------------------------------------------------------------------
//例2. 動(dòng)態(tài)數(shù)組的引用:
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))); //當(dāng)然 Arr2 維數(shù)也會(huì)是 6
ShowMessage(IntToStr(SizeOf(Arr1))); //4, 其實(shí)動(dòng)態(tài)數(shù)組是個(gè)指針
ShowMessage(IntToStr(SizeOf(Arr2))); //4
Arr2[5] := 99; //現(xiàn)在它們指向同一個(gè)數(shù)組, 改變這個(gè)就是改變那個(gè)
ShowMessage(IntToStr(Arr1[5])); //99
Arr1 := nil; //釋放其中一個(gè)指針, 數(shù)組繼續(xù)存在
ShowMessage(IntToStr(Arr2[5])); //99
end;
--------------------------------------------------------------------------------
//例3. 數(shù)組 Copy <1>:
var
Arr1,Arr2: array of Integer;
begin
SetLength(Arr1,6);
Arr1[5] := 100;
Arr2 := Copy(Arr1); //數(shù)組 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, 沒有一點(diǎn)牽扯了
end;
--------------------------------------------------------------------------------
//例4. 數(shù)組 Copy <2>:
var
Arr1,Arr2: array of Integer;
i: Integer;
begin
SetLength(Arr1,6);
for i := Low(Arr1) to High(Arr1) do //給每個(gè)元素賦值
Arr1[i] := i+1;
Arr2 := Copy(Arr1,1,3); //只 Copy 第2..4個(gè)元素
ShowMessage(IntToStr(Arr1[1])); //2, 現(xiàn)在 Arr2[0] 和 Arr1[1] 的值是一樣的
ShowMessage(IntToStr(Arr2[0])); //2
ShowMessage(IntToStr(Length(Arr1))); //6, 維數(shù)肯定不一樣了
ShowMessage(IntToStr(Length(Arr2))); //3
end;
--------------------------------------------------------------------------------
//例5. 動(dòng)態(tài)多維數(shù)組:
var
Arr: array of array of Integer; //定義多維數(shù)組
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
{這是給變體數(shù)組使用的,可能會(huì)有效率問題}
end;