Nick Sieger: Why does an array index start at 0, not 1 http://blog.nicksieger.com/articles/2006/07/27/why-does-an-array-index-start-at-0-not-1 en-us 40 do what you love "Why does an array index start at 0, not 1" by Hasan <p>Think of array indices as distance from the first element. The first element is 0 elements away from itself, the second, 1. This is where the zero-based array comes from.</p> Thu, 07 Sep 2006 18:02:17 +0000 urn:uuid:edc0469b-cf1d-4337-8490-3d92530d0d39 http://blog.nicksieger.com/articles/2006/07/27/why-does-an-array-index-start-at-0-not-1#comment-52 "Why does an array index start at 0, not 1" by Nick <p>Wow you guys are really giving this some serious thought. I appreciate the responses, even though the original post was supposed to be linking to some rather humourous discussion.</p> <p>MonkeeSage: have you read <a href='http://www.amazon.com/gp/amabot/?pf_rd_url=%2Fexec%2Fobidos%2Ftg%2Fdetail%2F-%2F0195142373%2Fref%3Dpd_cp_b_title%2F002-5020773-6923252%3F%255Fencoding%3DUTF8%26v%3Dglance&amp;pf_rd_p=168736601&amp;pf_rd_s=center-41&amp;pf_rd_t=201&amp;pf_rd_i=0140296476&amp;pf_rd_m=ATVPDKIKX0DER&amp;pf_rd_r=1A77ZZGFX88QKBZRVQJW' rel="nofollow">The Nothing That Is</a>?</p> Tue, 01 Aug 2006 21:50:49 +0000 urn:uuid:dbad8d1a-d494-45e5-baf4-994a1010ab46 http://blog.nicksieger.com/articles/2006/07/27/why-does-an-array-index-start-at-0-not-1#comment-43 "Why does an array index start at 0, not 1" by MonkeeSage <p>Not to steal your topic Nick, but I thought about a numbering system a few years ago that would recognize two dimensions: space and object. Object would not be considered the only &#8220;real&#8221; entity. So, to subtract, rather than thinking in terms of &#8220;taking away objects&#8221; you&#8217;d think in terms of the correlation between objects and space. </p> <p>For example, let&#8217;s say you have six total &#8220;things&#8221; (objects and spaces) laid out like this (where &#8220;|&#8221; is a space for any object and &#8220;X&#8221; is an object):</p> <p>XXX||| (=3[0]/3[s])</p> <p>There are three objects and three spaces. Now subtraction (on the object plane) can be reduced to the identity of adition (on the space plane): 3[o] - 2[o] (XXX - XX) &lt;=> 3[o] + 2[s] (XXX + ||). Assuming a fixed quantity for the object/space relationship, both equations resolve to the same solution: 1[o]/5[s].</p> <p>In this construct, zero is unambiguous: 0[o]/0[s] means: absolute nothing, a practiaclly impossible and therefor meaningless entity&#8211;do away with it!</p> <p>Heh. Well that was my attempt as being smart for the decade. I&#8217;m starting to smell burning electronics now so I&#8217;ll stop. BTW, you have a really nice blog, your AJAX is very responsive!</p> Sat, 29 Jul 2006 09:06:39 +0000 urn:uuid:ad5e50de-738d-43b5-b7a9-1ef5e04ce0f3 http://blog.nicksieger.com/articles/2006/07/27/why-does-an-array-index-start-at-0-not-1#comment-40 "Why does an array index start at 0, not 1" by evan <p><br />I justify it by thinking of it as the position of the element, not the number. Array elements have the same size, which means you could use the number and be just as accurate. But most things that have position do not have identical size, so you have to specify the start position to indicate which one you mean. This fits with the way arrays are stored in C, too, since you can multiple the element size by the index and not waste space.</p> <p>That doesn&#8217;t mean it isn&#8217;t confusing though. My string slices are always off by one.</p> Sat, 29 Jul 2006 00:07:36 +0000 urn:uuid:96278199-db51-4719-9f38-afc4c4d2fc79 http://blog.nicksieger.com/articles/2006/07/27/why-does-an-array-index-start-at-0-not-1#comment-39 "Why does an array index start at 0, not 1" by MonkeeSage <p>Because 0 is considered by most mathematicians to be a real number between -1 and 1 [<a href='http://en.wikipedia.org/wiki/0_(number)' rel="nofollow">ref</a>], and so in languages where arrays are positively indexed, zero is the first item (-1 is not possible, the first possible value then is 0).</p> <p>I can&#8217;t say that it is very intuitive or that it matches reality (even though maths is supposed to map to reality): you can&#8217;t have zero of a thing, because by definition zero is none of everything.</p> <p>In the context of comparison it makes sense and matches reality (i.e., you can have &#8220;space&#8221; for one apple in your basket, but not have an apple in it, and thus have 0 apples in your basket, or 1 space); but as a number like &#8220;0 apples, 1 apples, &#8230;&#8221; it makes no sense, and thus it has special rules from all other numbers (e.g., it is the only number for which 0 + n = n, and 0 * n = 0 is always true).</p> <p>But assuming that 0 is a real number, it does follow that positevly indexed arrays would start with it.</p> Fri, 28 Jul 2006 20:21:21 +0000 urn:uuid:52017ee1-08a4-41f7-bce4-a1cd2be42ded http://blog.nicksieger.com/articles/2006/07/27/why-does-an-array-index-start-at-0-not-1#comment-38 Why does an array index start at 0, not 1 <p>If you were looking for an answer to this question, you apparently cannot get a straight one in the <a href="http://forum.java.sun.com/thread.jspa?threadID=357877&amp;start=0&amp;tstart=0">java forums</a>. Just one of many choice quotes:</p> <blockquote> <blockquote> <p>Anyway, any logically counting would start at 0.</p> </blockquote> <p>I tried that once. I referred to my son as &#8220;Number Zero Son&#8221;. Got a definitely negative response.</p> </blockquote> Thu, 27 Jul 2006 14:57:00 +0000 urn:uuid:a41e4385-8403-4baf-b382-bff2382ece22 Nick Sieger http://blog.nicksieger.com/articles/2006/07/27/why-does-an-array-index-start-at-0-not-1 random qotd java http://blog.nicksieger.com/articles/trackback/37