You can also define integer-valued named constants, and use those if you prefer.
You can also define integer-valued named constants, and use those if you prefer.
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.
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].
Pascal, Julia, R, Lua use more natural 1-based, where you call the first element first.
(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!)
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:
T* table = (T*)malloc(sizeof(T) * 26) - 'a'
So that the characters can be used naturally without later modification (not a feature in every language, though).- Manually calculate each offset every time:
table[c - 'a'] = ...
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.
table['a']
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).
Where are you getting that from? That's not the characteristic feature of Arrays, that's the characteristic feature of Doubly Linked Lists.
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.