線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:5327
推到 Plurk!
推到 Facebook!

動態宣告Array

答題得分者是:SKYSTAR
bibo
一般會員


發表:10
回覆:9
積分:3
註冊:2002-06-14

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-01-16 15:43:17 IP:211.22.xxx.xxx 未訂閱
動態宣告一維陣列時, 其語法為 setLength(陣列名,陣列大小) 但,若我想宣告二維陣列時, 要如何宣告呢? MyArray=array[0..3,0..3] of integer 這是靜態的方法, 若我要的是動態的呢?? 可以嗎?
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-01-16 15:55:14 IP:147.8.xxx.xxx 未訂閱
You should get lot of articles using the keyword "SetLength" to search in this forum < href="http://delphi.ktop.com.tw/topic.php?topic_id=23024">http://delphi.ktop.com.tw/topic.php?topic_id=23024
cmf
尊榮會員


發表:84
回覆:918
積分:1032
註冊:2002-06-26

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-01-16 16:28:04 IP:61.218.xxx.xxx 未訂閱
引言: 動態宣告一維陣列時, 其語法為 setLength(陣列名,陣列大小) 但,若我想宣告二維陣列時, 要如何宣告呢? MyArray=array[0..3,0..3] of integer 這是靜態的方法, 若我要的是動態的呢?? 可以嗎?
type TIntegerArray :array of integer; TMyArray:array of TIntegerArray ;
------
︿︿
SKYSTAR
中階會員


發表:76
回覆:198
積分:64
註冊:2002-06-10

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-01-17 11:33:42 IP:211.74.xxx.xxx 未訂閱
您好:    哈...這一個問題...在我昨天看書時,剛好有看到...如下:
var
  MyArray: array of array of integer;  // 二維陣列
  ......
begin
  SetLength(MyArray, 4, 5);
  MyArray[0,1] := 10;
  MyArray[1,4] := 30;
  .....
end;
試試看吧!!
阿子
站務副站長


發表:120
回覆:230
積分:201
註冊:2002-03-18

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-01-17 14:27:45 IP:61.221.xxx.xxx 未訂閱
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

發送簡訊給我
#6 引用回覆 回覆 發表時間:2003-01-22 19:45:24 IP:61.217.xxx.xxx 未訂閱
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

發送簡訊給我
#7 引用回覆 回覆 發表時間:2003-01-23 15:42:57 IP:211.23.xxx.xxx 未訂閱
宣告動態陣列: var DArray:Array of Array of integer; begin Setlength(DArray,3,4); //3*4的陣列 end;
系統時間:2024-04-25 17:03:34
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!