8.1 Java Script - One-Dimensional
Arrays
OBJECTIVE
DISCUSSION
Array Fundamentals
-
Stores a series of related items
-
One or multi-dimensional
-
1D is like a list
-
2D is like a table
-
3D is like a cube
-
Use index (or indices) to access array elements
-
First item has index of 0
-
Typically use with loops to assign and retrieve elements
-
Examples...
1D array
|
Index |
Item |
|
0 |
Jo |
|
1 |
Bob |
|
2 |
Skippy |
|
... |
... |
|
N |
Bif |
2D array
|
|
0 |
1 |
2 |
... |
N |
|
0 |
a |
b |
c |
... |
d |
|
1 |
e |
f |
g |
... |
h |
|
2 |
i |
j |
k |
... |
l |
|
... |
... |
... |
... |
... |
... |
|
M |
m |
n |
o |
... |
p |
Using Arrays
var
sNames = new Array() //creates with 0 elements
var
iNumbers = new Array(4) //array with 4 elements (0 to 3)
var fVars =
new Array(3.14158, 6.02, 2.54) //3 elements
sNames[0] = "Jo"
iNumbers[3] = 4
fVars[1] = 6.023
alert(iNumbers[3])
var sQ =
sNames[0]
alert(sNames[iNumbers[3])
Array Methods and Properties
-
An array is an object with properties and methods
-
.sort sorts an array alphabetically
var myArray =
new Array(3,11,4,2)
myArray.sort()
//Arranges elements as 11,2,3,4
-
.length returns the number of elements in an array:
var myArray =
new Array("c", "a", "b")
alert(myArray.length)
//will display 3
EXERCISES
1. Write statements to accomplish the following:
-
Declare an array called iA to hold 5 numbers.
-
Declare an array called iB to hold the numbers 1, 3, and 4.
-
Declare an array called sC to hold the strings "1", "2", and "4".
-
Declare an array called fD. You don't know how many items will be in the array.
-
Assign 5 to the very first element in array iB in b above.
-
Assign 10 to the very last element in array iB in b above.
-
Assign "a" and "b" to the array fD in d.
-
Display the 2nd element of array sC in an alert.
-
Display the length of array sC in an alert.
-
Sort array sC.
2. In this exercise you will experiment with an array.
-
Create a page with a button.
-
Add a script block in the head section.
-
Define a global array called
myArray. Do not specify how many items it will hold.
-
Define a function called
display().
-
In the function, add one line of code to display the length of
myArray in an alert. Also add one line to display the last element in the
array using alert(myArray[myArray.length
- 1]).
-
Run the program. What is the length of myArray? What is
the last element in the array?
-
Modify the declaration of myArray so that it holds 4
elements. Then run the program. What is the length of the
array? What is the last element?
-
Add a line of code to set the last element to "a". Run
and test the program.
-
Comment out the line you added in h.
-
Modify the declaration of myArray so that it contains "a",
"b", and "c".
-
Run and test the program. How many elements does it
have? What is the last element?
-
Add a statement to add "d" to the array. Run the
program. How many elements does it have and what is the last element?
3. Create a page that allows the user to add items to an array,
display all of the items, display the length of the array, and sort the
array. Hint: To display the array you might want to create a global
variable gCounter to keep count of the number of clicks of the Display
button. Then when the user clicks the Display button you can
increment gCounter and display the element in the array at position gCounter.
LINKS AND HELP