Introduction to Arrays
So far, we have had small amount of variables in our program.¶
This is manageable.
But, what if we need more? A lot more.¶
This is not manageable.
Or, what if we don't know how many variables we need?¶
Is this even possible?
Remember this?¶
53711: Males: 5 Females: 2 53712: Males: 9 Females: 9 53713: Males: 11 Females: 3 53714: Males: 4 Females: 6 53716: Males: 1 Females: 0 Total Males: 30 Total Females: 20
In order to do this program we needed a lot of variables!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
What would happen if the zip codes changed? If more where added or removed?
What would happen if we got a very large file and we didn't know what zip codes were in the file?
Like one of the project 5 parts!
We need a more flexible data storage system!¶
Creating an Array¶
All modern programming languages have ways to store data in variables that are flexible, adaptable, easy to use, and very similar to every other programming language. The first of these storage systems we are going to learn is called Arrays
.
Here is an array
of zip codes. The code is creating a variable and assigning an empty array to it.
1 2 3 |
|
-
We use a set of square brackets
[]
to make the array. There are no spaces between the brackets. -
What does this really create? Well, an
Array
is an object. You will learn lots more about objects in the coming semesters. For now, think of an Array as a piece of running JavaScript code that lets us add data to it. -
Unlike a variable, we can store many pieces of data in it - convenient!
Adding items to an array¶
Let's make an array to hold student numbers.
1 |
|
At this point, the array is empty. We have a few ways of adding items (elements) to an array. Here's is one method. Arrays are indexed by numbers that start at zero. The first slot is named studentNumbers[0]
.
1 2 |
|
Let's add another element to the array. We need to use the next slot, which is studentNumbers[1]
.
1 2 |
|
All this code together, look like this:
1 2 3 4 5 |
|
We now have two student numbers in our array.¶
A shortcut¶
There is a nice way of doing this with less typing. The below code adds the same elements in one line. You could continue adding elements by separating them by commas.
1 |
|
Accessing elements in an array¶
Each individual student number in the array is called an array element
. And each element has the same name, studentNumbers
. How will we get the individual elements that we need out if they have the same name?
We use the index!
The index of an array starts at zero. This index identifies the location of each element in the array.
1 2 3 4 5 |
|
The Length Property¶
Remember the length property? We used this property to find the number of characters in a String. We can also use it with an array to get the number of elements stored in the array. It is very useful!
1 2 3 4 5 6 7 8 |
|
In our example array, this would give us 2
. The length of an array is ALWAYS one more than the last index. So, the length also gives us the next available index!
Adding elements with the length property¶
Using the length property we can also add element to an array. This is better than using hard-coded index values because we often don't know what the next available slot is.
1 2 3 4 5 6 7 |
|
The .push()
method¶
There is one more way to add an element to an array. Since an array is an object, there are methods we can use with it. One of them is the .push()
method.
It automatically adds elements to the next available index for us - nifty!
1 2 3 4 5 6 7 |
|
Quick Recap¶
This is GOOD stuff, you might want to book mark it
Creating an array
1 |
|
Creating array shortcut
1 |
|
Adding elements
1 2 3 |
|
Adding elements with the length
property
1 2 3 |
|
.push()
method
1 2 3 |
|
1 2 3 4 5 6 |
|
Looping Arrays¶
Let's start by looking at a complete function. It create an array of area codes and displays them to the page. Does this look any more efficient that using regular variables?
What kind of issues do you see with this code? What would you have to do if we changed, added, or deleted an area code?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
Adding a Loop¶
Since the only thing that changes in lines 27-32 in the above version is the value of the index number, we could set this up as a loop!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
How did that work?¶
We took advantage of the length property and the index value to loop through the array. This is more flexible and maintainable.
The loop continues until the index is incremented to 7, which will return a false
, which stops the loop.
A Better Version¶
We can take advantage of the shortcut of adding elements to an array to remove some lines of code.
1 2 3 4 5 6 7 |
|
Reduce to one line. Not only is this smaller, but it's more readable.
1 2 |
|
Final Version
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Viewing All Array Elements¶
It is possible to display the contents of each and every "slot" of an array as just a comma delimited list. This is fine for debugging, but rarely if ever would this be acceptable as a format to leave in a production program for users to see and have to work with.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Notice how this looks just like the data on line 12?
Array Summary Page¶
Helpful "cheat-sheet" for using arrays.
Read more¶
Related Reading
Pages 145 - 151
Hands on Work¶
Labs
- Lab06: First Arrays
- unit05/labs/lab-06-arrays.html
- Lab07: More Experiments with esLint
- unit05/labs/lab-07-esLint.html
Exercises
- Exercise for Lab06
- unit05/exercises/exercises-06.html