動態宣告Array |
答題得分者是:SKYSTAR
|
bibo
一般會員 發表:10 回覆:9 積分:3 註冊:2002-06-14 發送簡訊給我 |
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
|
cmf
尊榮會員 發表:84 回覆:918 積分:1032 註冊:2002-06-26 發送簡訊給我 |
|
SKYSTAR
中階會員 發表:76 回覆:198 積分:64 註冊:2002-06-10 發送簡訊給我 |
|
阿子
站務副站長 發表:120 回覆:230 積分:201 註冊:2002-03-18 發送簡訊給我 |
Array 的使用
low()//取出陣列的起始值 (目前使用只能取回有一維的起始值) high()//取出陣列的最後值 (目前使用只能取回有一維的最後值) //固定大小陣列 //宣告 //一維 A : array[i1..i2] of {形態} //設定範圍 //二維 多維以此類推 A : array[i1..i2,i3..i4] of {形態} //動態陣列 //動態陣列起始值一定是從0開始 //當動態陣列重新設定長度(大小)後,其原本在在陣列中的值(資料)不會 // 不見,但只有在陣列變大。 //宣告 //一維 A : array of {形態} SetLength(A,i) //重新設定陣列大小 //二維 多維以此類推 A :array of array of {形態} SetLength(A,i1,i2) //重新設定陣列大小 多給參數 //陣列COPY //例一 a,b :=array of {形態}; b := Copy(A); //用Copy 不加參數則會將 A 陣列全部Copy 到B陣列去 //例二 b[1] :=Copy(a,1,1); //加參數可取單一值 //使用一個二維陣列(s[x,y])時使用S[x]可以取回有X個的一個陣列, //此時S[X]傳回的是一個陣列而不是文字,而此陣列個數為Y個。 S :array[0..2] of array[0..3] of string; s[0] = 3個 array[0..3] of string s[1] = 3個 array[0..3] of string s[2] = 3個 array[0..3] of string從思考取勝一切~q
------
從思考取勝一切~q |
flyup
資深會員 發表:280 回覆:508 積分:385 註冊:2002-04-15 發送簡訊給我 |
Dynamic arrays are arrays that can grow or shrink at runtime to accommodate more or less elements. The declaration of a dynamic array is much like the declaration of a static array, except that the index range is omitted. For example: var
a: array of integer; A dynamic array initially has no elements. You should use the SetLength procedure in your code to assign the number of elements needed. You can call SetLength many times as needed. If the new length is greater than the current number of elements, space for new elements is added, and if it's smaller then the last elements of the array are disposed. For example SetLength(a, 10) assigns space for 10 elements. Elements in a dynamic array are indexed starting from 0 to one less the Length of the array, so for example the following code can be used to initialize the array elements to zero: for i := 0 to Length(a)-1 do
a[i] := 0; In the example attached to this article you'll see the use of a dynamic array to store the names of the files in a folder. Naturally, a string list would be better for the task, but a dynamic array was used for a pedagogic purpose. Multimensional dynamic arrays A two-dimension dynamic array can be declared this way: var
a: array of array of integer; This way you can assign both dimensions at runtime. The SetLength procedure admits as many NewLength parameters as dimensions. For example SetLength(a, 10, 10) would set the size of the array to an square of 10 x 10. One interesting thing is that instead of seeing your array as a matrix (i.e. rectangular) you can see it as a vector of vectors that don't necessarily have to have the same number of elements. For example the following code creates a triangular array and initialize its elements with consecutive numbers: k := 0;
SetLength(a, 4); // 4 rows
for i := 0 to 3 do begin // For each row
SetLength(a[i], i 1); // Set the number of columns
for j := 0 to i do begin // Initialize the row
a[i,j] := k;
inc(k);
end;
end;
0 1 2 3
---
0 | 0 |
--- ---
1 | 1 | 2 |
--- --- ---
2 | 3 | 4 | 5 |
--- --- --- ---
3 | 6 | 7 | 8 | 9 |
--- --- --- ---
----------------
局局棋盤步步新,
變化無常平常待。
人生相處平常心,
無憂無慮心事成。
----------------
|
kan0515
中階會員 發表:56 回覆:120 積分:50 註冊:2002-06-24 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |