[問題] 關於陣列從0或1開始算起的好處

看板Programming作者 ( U U)時間14年前 (2010/08/19 21:59), 編輯推噓1(1018)
留言19則, 7人參與, 最新討論串1/4 (看更多)
http://www.iis.sinica.edu.tw/~scm/ncs/2009/07/go-to-considered-harmful/ 上面這個連結雖然是討論GOTO 不過Dijkstra大師在裡面說 『我以為到了現在,一個專業程式員該有高一點的自我要求了,陣列應該從0算起, 我以為到了現在,一個專業程式員該知道自然數從 0 開始的好處了』 但是我在看Core Java的時候作者又說了 『Java counts the code units in strings in a peculiar fashion: the first code unit in a string has position 0. This convention originated in C, where there was a technical reason for counting positions starting at 0. That reason has long gone away and only the nuisance remains. However, so many programmers are used to this convention that the Java designers decided to keep it.』 如果可以拋開歷史因素之類相容問題的話 那麼陣列索引到底是從0開始計算好,還是從1開始計算好呢? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.50.39.188

08/19 22:09, , 1F
想也當然是從0開始算起比較好啊, 因為
08/19 22:09, 1F

08/19 22:10, , 2F
從0開始你可以依照實際應用決定要不要
08/19 22:10, 2F

08/19 22:11, , 3F
空下第一個元素來方便計算存取, 從1開
08/19 22:11, 3F

08/19 22:11, , 4F
始就不行
08/19 22:11, 4F

08/19 23:00, , 5F
我覺得單純只是arr+i*size好做而已...
08/19 23:00, 5F

08/20 00:10, , 6F
我覺得作者在胡扯.. LPH +1
08/20 00:10, 6F

08/20 00:10, , 7F
從0開始是有計算上的好處的,這點沒有改變
08/20 00:10, 7F

08/20 00:13, , 8F
如果是從java的角度來看, 轉成b-code
08/20 00:13, 8F

08/20 00:14, , 9F
時應該可以做點手腳, 這樣應該不會太
08/20 00:14, 9F

08/20 00:14, , 10F
難做吧?
08/20 00:14, 10F

08/20 00:15, , 11F
以下面 l 大那篇來解釋, 除了內建型態
08/20 00:15, 11F

08/20 00:16, , 12F
自訂類別都是陣列參考, 實作上不用一
08/20 00:16, 12F

08/20 00:17, , 13F
定要map到最開始, 浪費一個參考的空間
08/20 00:17, 13F

08/20 00:17, , 14F
應該不是大礙
08/20 00:17, 14F

08/20 00:53, , 15F
我聽大師的,Dijkstra +1 XD
08/20 00:53, 15F

08/20 00:54, , 16F
keep it simple有時還是很重要
08/20 00:54, 16F

08/20 08:01, , 17F
08/20 08:01, 17F

08/20 11:42, , 18F
樓上這篇講的不錯耶,很清楚
08/20 11:42, 18F

08/20 12:27, , 19F
這是品味問題,Java 那個品味差,所以沒差
08/20 12:27, 19F
文章代碼(AID): #1CRJZAQx (Programming)
文章代碼(AID): #1CRJZAQx (Programming)