The characteristic feature of Arays is that all but the last element have their next element and all but the first element have their previous element. So what would be the value of having or using indexes other than Integers to index Arrays?
You can also define integer-valued named constants, and use those if you prefer.
That sounds more like a linked list than an array.
In C, an array is just contiguous bytes, and you reach an index through math (starting location + (index * size)).
Pretty sure lots of languages still do this to some degree, as your lookup times are effectively zero, though altering the number of elements is way more complex/expensive than in a linked list.
> and you reach an index through math (starting location + (index * size)).
By the way, that's the only reason why C-derived languages use unintuitive zero-based indexing. There's really no other reason to call the first element a[0] instead of a[1].
Ah, but is it unintuitive though? Once one understands that in the vast majority of cases, C deals in pointers, not arrays, it makes perfect sense that indexing uses an offset rather than the nth element.
Yes, but that's only C. Languages that have no pointer arithmetic (JavaScript, Python, Java, Rust, Go you name it) got that from C even though they didn't have to.
The index of the first element is always 0 plus the index of the first element. This means it sort-of doesn't matter what the index of the first element of the array is - but, viewed another way, it's an excellent argument for 0. Starting at 0 means you don't need to add a constant in the (very common) case of dealing with a subarray that starts at the first element of the containing array.
Yep, exactly. My point is all that is about pointer arithmetic, even though most higher-level languages don't provide you with pointer arithmetic (and their arrays may have nonzero memory offsets anyway).
Pascal, Julia, R, Lua use more natural 1-based, where you call the first element first.
But the first element is first whatever index you choose. Why 1, rather than, say, 99, or -3?
(You should ideally be able to choose any of these options, and others - e.g., 5, 7, -200, 14,771,589,734,598,742,923,574,835, etc. - because they all make exactly as much logical sense as 1 does and any of them is as elegant a choice as 1 is. But if you get no choice, it absolutely has to be 0, because it's the only non-arbitrary option. No other number guarantees to do nothing to the value you add it to!)
> So what would be the value of having or using indexes other than Integers to index Arrays?
It removes a level of indirection, wasted space, and mental overhead. Suppose you have some table of 'a'..'z' -> whatever. What are your options?
- Switch/case. This may be optimized and fast, but that's not guaranteed across all languages and compilers.
- Hash map. This introduces memory overhead even if it is technically O(1) for access times.
- If in C, you could do:
So that the characters can be used naturally without later modification (not a feature in every language, though).- Manually calculate each offset every time:
With language appropriate type conversions, if needed.Or you can allow any custom range to work, and do the math in that last option automatically because it's trivial for a compiler to figure it out and it removes unnecessary mental overhead from the program reader and writer.
Just works.> You can also define integer-valued named constants, and use those if you prefer.
Because everyone wants to see `lower_case_a = 0` and the 25 other cases in their code along with all other possible constants. At least use an enum, sane languages even make them type-safe (or at least safer).
> The characteristic feature of Arays is that all but the last element have their next element and all but the first element have their previous element.
Where are you getting that from? That's not the characteristic feature of Arrays, that's the characteristic feature of Doubly Linked Lists.
I wouldn't call it a characteristic feature of either. Any ordered, finite sequence of elements has those properties. Doubly linked lists simply offer O(1) access to "next elements" and "previous elements" given an element.
Use the index 'n' of any element of the array. Unless that is the first element you can always take the elment n - 1 of the same array. That is the "previous element" of the elment at n. And so on.
I'm not saying that if you have value of any element you could derive the value of the previous or next element from it. I'm saying "it has a previous element" (unless it is the 1st element) meaning there is such a previous element and it is also possible to access that previous element if you know the index of the current element under investigation.