How can we change the first index of an array to 1? Pls tell | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can we change the first index of an array to 1? Pls tell

I'm asking coz it's very inconvenient for 0 to be the first

18th May 2020, 5:10 AM
Daksh
Daksh - avatar
3 Answers
+ 1
There is only a limited amount of languages that use 1 and not 0, I can name Lua and MatLab. It is not a very smart choice, because when it comes to using minuses, then 0 is a neutral number, hence why we start with it. If you still want to somehow change it, the best you can do is make a custom class in whatever language you are using that takes care of array handling, like a List and start from 1.
18th May 2020, 5:17 AM
coddy
coddy - avatar
0
Sorry it is for java
18th May 2020, 5:22 AM
Daksh
Daksh - avatar
0
If you are using a OOP language you can create you special array class that extends the built-in array class. If you don't like OOP, there are workarounds depending on the language. For example you can create an associative array whose first key is 1. It is worth mentioning that in some languages like Lua arrays start at 1 by default.
18th May 2020, 5:29 AM
Ore
Ore - avatar