4

Something that I noticed about C#/Java is this seemingly (to me at the moment) inconsistent issue with array size declaration and the default first-index of array sizes.

When working with arrays, say you want to create a new integer array size 3, it would look like this:

int[] newArray = new int[3] {1, 2, 3};

Totally find and readable... Right?

The standard with programming languages seem to dictate that the "first" index is 0.

Using that logic, if I am interested in creating an array the size 3, I should really be writing this:

int[] newArray = new int[2] {1, 2, 3};

Wait a minute.. VS is throwing an error, saying an array initialize of length 2 is expected.

So there's an inconsistency with the first index in looping through an array and the array-size declaration? The former uses a 0-th based index, and the second a 1-th index.

It's not game-breaking/changing in any form or way, but I'm genuinely curious why there's a discrepancy here, or hell, if this is even an issue at all (like I say, it's not game-breaking in any way, but I'm curious as to why it's done this way).

I can at the moment think of reasons why 1-th based index would be used:

In a for-loop you would use < newArray.Length as opposed to < newArray.Length - 1 or < newArray.Length.

Working with Lists for awhile and then coming back to size-needs-to-be-declared-arrays caught me a bit off-guard.

theGreenCabbage
  • 5,197
  • 19
  • 79
  • 169
  • You don't even have to declare the length if you use a static array initializer: `new int[] { 1, 2, 3 }` – fge Mar 19 '14 at 16:13
  • 2
    No offense, but I don't think you really thought this one through. The length is not an index of any kind, neither 0-based nor 1-based. – harold Mar 19 '14 at 16:18
  • You're not wrong -- I definitely am not seeing the distinction between the length AND the index, and I am glad this post can clear it up. – theGreenCabbage Mar 19 '14 at 16:19
  • To put many fancy words behind what @harold is saying, you are confusing [cardinal numbers and ordinal numbers](http://english.stackexchange.com/questions/28314/whats-the-difference-between-cardinal-and-ordinal-as-adjectives). –  Mar 19 '14 at 16:19
  • If you want an array with bounds [-15, -5] should you create it with -5 elements? :) (not every language forces 0-based indexing, even .Net onces - check out http://stackoverflow.com/questions/1301276/what-is-the-purpose-of-array-getlowerboundint ) – Alexei Levenkov Mar 19 '14 at 16:21
  • @harold Upon further thought, I think you totally understood the point of my question. `The length is not an index of any kind, neither 0-based nor 1-based`. Though one thing -- isn't it technically 1-based? – theGreenCabbage Mar 19 '14 at 16:24
  • @theGreenCabbage He was saying it's not an *index* and hence the concept of 0- or 1-based makes little sense. I don't think there even is a convention for how to interpret 0- or 1-based for cardinal numbers. If you take it to mean the smallest number that's valid, then it's zero-based: [Arrays can have length zero](http://ideone.com/fVr97O). –  Mar 19 '14 at 16:27

5 Answers5

15

Because you are declaring the array with the number of elements it will contain.

I am unsure how that is inconsistent.

How many times do you have to saw to cut a log in 3 pieces? Hint: not 3 times.

Also note how in your post title you incorrectly refer to the array size declaration as 'index'.

Wim
  • 11,998
  • 1
  • 34
  • 57
5

I think you are confusing the indexer with the length. You want to hold three elements (or variables) in your array, denoted by

...new int[3]...

and the elements in the curly brackets are the values, not the index. Index is still 0-based. Longform would look like this:

int[] newArray = new int[3];
        newArray[0] = 1;
        newArray[1] = 2;
        newArray[2] = 3;

So in that, you can see where your zero-based index is as it relates to the values of your int[].

Babak Naffas
  • 12,395
  • 3
  • 34
  • 49
Neurothustra
  • 300
  • 1
  • 5
  • 14
3
int[] newArray = new int[2] {1, 2, 3};

In English, this would translate you "I want a container that can hold 2 items but put 3 items in it". You are confusing the length of an array (how many items it can hold) with the index into the array which is 0-based in C-based languages (ex. C, C++, C#, Java, Javascript, Swift).

Also, consider what an array index really is (atleast with low level languages like C); it's an offset in memory from the base address of your array variable. So arr[n] translates to "take the address for arr, advance n * (the size of my type) bytes in memory, and give me the value at that calculated address. So when n = 0, you are referencing the value at the base memory address (the beginning of the array).

Babak Naffas
  • 12,395
  • 3
  • 34
  • 49
  • 1
    I was _going_ to ask if Java was really considered a C based language, but googled, and discovered that my view of truly C base languages was really myopic... (***[see this](http://en.wikipedia.org/wiki/List_of_C-based_programming_languages)***) – ryyker Mar 19 '14 at 16:21
  • 1
    For people who find 0 based indexes confusing, the second paragraph is something they probably need to learn and understand. – Dolphin Jul 02 '14 at 16:52
2

Answer provided by Wim Hollebrandse is absolutely great and correct, but wanted to expand on it a little bit to give OP a bit more understanding why indices start with 0 in most (but not all) languages.

As Wim stated, at declaration it is how many elements will be stored in an array, and that's quite palatable for human understanding. What confuses many is why 1st item is actually referred to as 0th (i.e. index 0)... The reason for this is the simple math that is needed to find seeked element in array.

All elements in array are stored sequentially in contiguous block. If the array is located for example at address 100 and hold integers (each integer of size 4 bytes), then when looking for the very first item, it would be located in the beginning of array, that is at address 100. The second item will be stored immediately after the first, or 100+4=104 address. 3rd item is stored after 2nd or at address 108.

So to calculate position of I-th element, if indices begin with 0, the math is simple:

I-th-address = address_of_array + I * sizeof(datatype)
e.g. for our example it is 100 + i * 4

if indices begin with 1, then math requires more operations:

Ith-address - address of array + (I-1) * sizeof(datatype)

... which is less efficient and unnecessary than the 0-based.

LB2
  • 4,802
  • 19
  • 35
1

You are mixing the declaration of the array with the request of an array element. While declaring an array you specify the length of the array (number of elements), and this is 3, correct. When you request an element, the you specify the index, and the first index is 0. But you shouldn't mix the index and the length of the array.

In a for-loop, you should take < newArray.Length: If you have a length of 3, then the loop will start at 0 and go through index 0, 1 and 2. Then it stops, because 3 is not < 3.

Kevin Brechbühl
  • 4,717
  • 3
  • 24
  • 47