Re: [問題] 關於陣列從0或1開始算起的好處
我覺得首要的還是以這程式語言的使用環境要求
例如在一些低階(要求速度的組合語言、一般的單晶片開發)環境
陣列從0起算是很正常的,因為不這樣的話會白白增加程式碼長度
另外,GOTO自然是用得更兇了,反正編譯之後都是一堆JMP
只要加上適當的旗標,GOTO沒那麼難搞
但是到了大型(數千數萬行)的專案(程式設計師合作)開發的時候
程式碼最重要的是可讀性可流通性那些
GOTO不是不能用,但就僅限於特殊要求程式碼效率的小Function
至於陣列該怎麼起算? 好用,程式碼能夠乾淨簡潔才是最重要的
: : 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開始計算好呢?
在VB6中如果你宣告一個 Array(10)
指的是Array(0~10)都可以用,共有十一個空位,你可以從0算到9或是1算到10都可以
原因很單純的就是不差那一點記憶體空間(VB.Net我不確定)
在Fortran中如果宣告Array(10), 可用空間則是1~10
在C、JAVA的話,則都是0~9
--
填充題: ______なのはいけないと思います!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.116.180.163
推
08/20 06:20, , 1F
08/20 06:20, 1F
→
08/20 09:15, , 2F
08/20 09:15, 2F
推
08/20 11:40, , 3F
08/20 11:40, 3F
→
08/20 12:10, , 4F
08/20 12:10, 4F
→
08/20 12:11, , 5F
08/20 12:11, 5F
推
08/20 12:17, , 6F
08/20 12:17, 6F
→
08/20 16:52, , 7F
08/20 16:52, 7F
→
08/20 16:52, , 8F
08/20 16:52, 8F
→
08/20 17:00, , 9F
08/20 17:00, 9F
推
08/21 14:35, , 10F
08/21 14:35, 10F
→
08/21 14:35, , 11F
08/21 14:35, 11F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 3 之 4 篇):