Understanding and implementing arrays
So what are arrays? Well, they are an alternative way of defining variables that allow multiple variables to be quickly declared, painlessly. If you're a little confused at this stage, its ok.
Lets first quickly go over the "standard" way of declaring variables. Assuming we want to declare two variables to store the names of your in-laws...here's how it could be done:
Pure, right? But not very simple. In order to define 50 variables, We had to think of a unique variable name for each one (such as x, y, n, and so on) while keeping track of which variable we're currently on. Well, with arrays, that kind of hassle is non-existent.
Just another friendly reminder: arrays are merely another way of defining variables, and nothing more. Lets first see how an array in general is created. To define an array, do the following:
var arrayname=new Array(# of variables to be created)
var x=new Array(50)
Now, to store the names into this array, we would do the following instead:
x="Sam" x="Bob" x="Joe" x="Peter" x="May" " " " x="George"
Are you starting to see the advantages arrays have over the standard method when declaring large chunks of variables?
Some advantages of arrays:
- No need to provide each variable with a unique name.
- Easier tracking and reference to any of the 50 variables.
The biggest advantage of storing variables in arrays is undoubtedly when it comes to referencing them...you can now literally yell: " tell me who is the 25th person down my family tree!" and you're get an answer easily:
That kind of versatility is quite difficult without arrays.