Beruflich Dokumente
Kultur Dokumente
JavaScript: Array
JavaScript: Arrays
2
Introduction
Arrays
Declaring and Allocating Arrays
Examples Using Arrays
Random Image Generator Using Arrays
References and Reference Parameters
Passing Arrays to Functions
Sorting Arrays
Searching Arrays: Linear Search and Binary Search
Multidimensional Arrays
Building an Online Quiz
Objectives
3
Introduction
Arrays
Dynamic
11.2 Arrays
Arrays in JavaScript
length property
Arrays
Name of array
c[ 0 ]
-45
c[ 1 ]
c[ 2 ]
c[ 3 ]
72
c[ 4 ]
1543
c[ 5 ]
-89
c[ 6 ]
c[ 7 ]
62
c[ 8 ]
-3
c[ 9 ]
c[ 10 ]
6453
c[ 11 ]
78
A 12-element array.
Arrays
Operators
Associativity
Type
left to right
highest
() [] .
right to left
unary
++ -- !
left to right
multiplicative
* / %
left to right
additive
+ left to right
relational
< <= > >=
left to right
equality
== !=
left to right
logical AND
&&
left to right
logical OR
||
right to left
conditional
?:
right to left
assignment
= += -= *= /= %=
Fig. 11.2 Precedence and associativity of the operators discussed so far.
Objects
Operator new
var c;
c = new Array( 12 );
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4
5
InitArray.html
(1 of 3)
-->
7
8
9
10
<title>Initializing an Array</title>
11
12
13
<!--
14
15
16
function initializeArrays()
17
18
19
20
21
22
23
n1[ i ] = i;
10
24
25
26
27
n2[ i ] = i;
28
InitArray.html
(1 of 3)
29
outputArray( "Array n1
30
31
32
33
34
35
36
37
38
document.writeln(
"<table
border
= \"1\" width
The second time
function
ouputArray
is =" +
39
40
41
42
43
"align = \"left\">Subscript</th>" +
11
44
45
46
47
theArray[ i ] + "</td></tr>" );
InitArray.html
48
(149 of 3) document.writeln( "</tbody></table>" );
50
51
// -->
52
</script>
53
54
55 </html>
12
Initializer list
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4
5
-->
InitArray2.html
(1 of 2)
7
8
9
10
11
12
13
<!--
14
function start()
15
16
17
18
19
"yellow", "black" );
20
var integers1 = [ 2, 4, 6, 8 ];
21
var integers2 = [ 2, , , 8 ];
22
23
24
25
26
15
27
28
29
30
31
InitArray2.html
(2 of 2)
32
33
34
"width = \"100%\">" );
35
36
"align = \"left\">Subscript</th>" +
37
38
39
40
41
theArray[ i ] + "</td></tr>" );
42
43
document.writeln( "</tbody></table>" );
44
45
// -->
46
</script>
47
48
49 </html>
16
18
statement
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4
5
SumArray.html
(1 of 2)
-->
7
8
9
10
11
12
13
<!--
14
function start()
15
The for loop sums the values contained in the 10element integer array called theArray.
16
var theArray = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
17
18
19
20
21
22
23
19
24
25
26
27
28
total2 );
SumArray.html
(2 of 2)
29
30
// -->
31
</script>
32
33
34 </html>
20
switch statements
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
RollDie.html
(1 of 2)
5
-->
7
8
9
10
11
12
13
<!--
14
15
16
// summarize results
17
18
19
++frequency[ face ];
20
21
23
22
23
"width = \"100%\">" );
RollDie.html
(2 of 2)
24
25
26
"Frequency</th></thead></tbody>" );
27
28
29
30
31
32
document.writeln( "</tbody></table>" );
33
// -->
34
</script>
35
36
</head>
37
<body>
38
39
40 </html>
24
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4
5 <!-- Fig. 11.7: RandomPicture2.html
-->
<head>
<title>Random Image Generator</title>
11
12
13
<!--
14
var pictures =
15
27
16
17
RandomPicture2
.html
(2 of 2)
18
19
20
// -->
21
</script>
22
23
</head>
24
25
26
27
<body>
<p>Click Refresh (or Reload) to run the script again</p>
</body>
28 </html>
28
Pass-by-value
Pass-by-reference
Passed by reference
Array.join
PassArray.html
(1 of 3)
32
PassArray.html
(2 of 3)
33
PassArray.html
(3 of 3)
34
Sorting Arrays
Sorting
Array.sort
Sort.html
(1 of 2)
Method sort takes as its optional argument the name of a
function that compares two arguments and returns a value
of 1, 0 or 1.
37
Sort.html
(2 of 2)
Function compareIntegers calculates the difference
between the integer values of its arguments.
38
Sorting Arrays
Linear search
Binary search
LinearSearch.html
(1 of 3)
41
LinearSearch.html
(2 of 3)
42
LinearSearch.html
Variable theArray gets the value of
Array a and variable key gets the
(3 of 3)
Function
linearSearch compares each
value of variable searchKey
.
each element with a search key.
43
BinarySearch.html
(1 of 5)
Array a is initialized with 15 elements.
45
BinarySearch.html
(2 of 5)
46
BinarySearch.html
(3 of 5)
If the key matches the middle element of a
subarray, the subscript of the current element is
returned.
If key is less than the middle element, the high
subscript is set to middle 1.
47
BinarySearch.html
(4 of 5)
Function buildOutput creates the markup that
displays the results of the search.
48
BinarySearch.html
(5 of 5)
49
Multidimensional Arrays
Two-dimensional arrays analogous to tables
Two subscripts
Multidimensional Arrays
Column 0
Column 1
Column 2
Column 3
Row 0
a[ 0 ][ 0 ] a[ 0 ][ 1 ] a[ 0 ][ 2 ] a[ 0 ][ 3 ]
Row 1
a[ 1 ][ 0 ] a[ 1 ][ 1 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ]
Row 2
a[ 2 ][ 0 ] a[ 2 ][ 1 ] a[ 2 ][ 2 ] a[ 2 ][ 3 ]
Column subscript (or index)
Row subscript (or index)
Array name
52
Multidimensional Arrays
Declaring and initializing multidimensional arrays
var b = [ [ 1, 2 ], [ 3, 4, 5 ] ];
Multidimensional Arrays
Also possible to use new operator
Create array b with two rows, first with five columns and
second with three:
var b;
b = new Array( 2 );
b[ 0 ] = new Array( 5 );
b[ 1 ] = new Array( 3 );
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4
5
InitArray3.html
(1 of 2)
-->
7
8
9
10
Array array1
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
two rows.
11
12
13
<!--
14
function start()
15
16
var array1 = [ [ 1, 2, 3 ],
17
[ 4, 5, 6 ] ];
18
19
[ elements
3 ],
20
[ 4, 5, 6 ] ];
rows.
// first row
// second row
outputArray
each arrays
// first displays
row
in a Web
page.
// second
row
// third row
21
22
23
24
55
25
26
27
28
29
InitArray3.html
(2 of 2)
30
31
32
33
34
35
36
37
38
document.writeln( "</tt>" );
39
40
// -->
41
</script>
42
43
44 </html>
56
Multidimensional Arrays
Represented as an array
XHTML Forms
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
Quiz.html
(1 of 2)
5
-->
7
8
<head>
10 <title>Online Quiz</title>
11
12 <script type = "text/JavaScript">
13
14
function checkAnswers()
15
16
17
if ( myQuiz.radiobutton[ 1 ].checked )
18
19
Determining
theagain"
value of
document.write( "Your answer is incorrect.
Please try
); property
20
21
checked.
22
23 </script>
24
25 </head>
59
26
27 <body>
28
29
30
Quiz.html
(2 of 2)
31
<br />
32
33
34
35
36
37
<label>Error-Prevention Tip</label>
38
39
40
<label>Performance Tip</label>
41
42
43
44
45
46
47
</p>
48
</form>
49 </body>
50 </html>
60