Android Question Newbie question about arrays - very simple

AlexOfOz

Active Member
Licensed User
I have been battling to improve my code by turning it into an array. I started by creating many "items" manually and manipulating them in the code. "For item number 1 do this. For item number 2 do this". As you would expect, it worked successfully and I had a smile.

Then I started improving my code by turning the many "items" into members of an array, and manipulating them as an array. Much better code, obviously. However, my (innocent, newbie) question is, am I correct in saying that an array ALWAYS assumes and expects and dictates that you are starting at zero and not 1?

The reason I ask is that my list of items that I created was from 1 to 25, but I could not get my array code to work properly with them. But when I tricked my array to see them as 0 to 24 it all worked successfully.

Is my assumption about arrays correct?

Thanks.
 

mangojack

Expert
Licensed User
Longtime User
am I correct in saying that an array ALWAYS assumes and expects and dictates that you are starting at zero and not 1?



Yes.

In B4X (and likewise BASIC language) all Arrays start from 0. ie: looping thru 25 elements would be ... For i = 0 To 24

From memory ... In Visual Basic you could set Arrays to start at 1 using the statement , Option Base 1 .


ps.. Cheers from WA.
 
Upvote 0

AlexOfOz

Active Member
Licensed User
You might have a look here: https://www.b4x.com/guides/B4xBasicLanguage/?page=12
Chapter 2.3.2 Array variables.
Thanks Klaus. I've learned a lot in the last 12 hours and have seen where I was going wrong. Arrays starting at zero was only part of it. I have now managed to reduce about 100 lines of code down to just a few. I am very happy and heading steadily to my next road block. That will be setting up read / write access to a file I'll be storing on the cloud. But that's a long way into the future. For now I'm just getting joy making pretty patterns on my phone - with arrays.
 
Upvote 0
Top