全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:812
推到 Plurk!
推到 Facebook!

請問 array 的一個怪問題

答題得分者是:william
tony115722
一般會員


發表:53
回覆:63
積分:23
註冊:2002-07-09

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-01-15 13:54:05 IP:211.22.xxx.xxx 未訂閱
小弟在使用以下方法時,發現很奇怪的事情耶... const   def_FieldName: array[1..19] of String = ('CUS_NO','CO_NM','CUS_NM','DR_NM','DL_CD','OUT_DL',   'MOD_CD','CH_NO','EN_NO','CAR_NO','S_ADR_CD','S_ADR2','HO_TEL','CO_TEL','M_PHONE','LA_SA',   'ASK_CD','CUS_ID','IV_NO');    var j:integer;     check:Boolean; begin   for j := 1 to 19 do begin     if def_FieldName[j]=Name then begin  --> j 的值居然是從19,18,17..1 check :=true; break; end else check :=false; end; result:=check; end; 不解...不是應該從 1,2,3..19 嗎? 初學者出沒注意!!
------
I like DeBug
william
版主


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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-01-15 14:03:07 IP:147.8.xxx.xxx 未訂閱
Watch j in debugger with optimization on? Because j is used only as a looping index and compiler does all the necessary mappings for you...
tony115722
一般會員


發表:53
回覆:63
積分:23
註冊:2002-07-09

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-01-15 15:39:31 IP:211.22.xxx.xxx 未訂閱
>>compiler does all the necessary mappings SORRY william 兄,這句話有點不解意思.. 初學者出沒注意!!
------
I like DeBug
william
版主


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

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-01-15 16:30:42 IP:147.8.xxx.xxx 未訂閱
Look at the following code:    
procedure TForm1.Button1Click(Sender: TObject);
var
    i: integer;
begin
    for i := 1 to 10 do begin
        Application.ProcessMessages;
    end;
end;
(1) snapshot from CPU window with optimization: (2) snapshot from CPU window without optimization: In 1, there are only dec, jnz compared to inc, cmp, jnz in 2. It is pretty clear that 1 is more efficient. Hence the compiler is smart enough in choosing form 1 for you. In you case, the compiler also knows about the constant array def_FieldName and j is used only as an index of it, thus it performs proper mappings and your checking should goes from 'CUS_NO' to 'IV_NO'.
tony115722
一般會員


發表:53
回覆:63
積分:23
註冊:2002-07-09

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-01-15 17:09:21 IP:211.22.xxx.xxx 未訂閱
哈...原來就是讀書時課本所說的一樣.. 我明白了..謝謝.william 兄.. 初學者出沒注意!!
------
I like DeBug
系統時間:2024-05-02 23:29:30
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!