Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 3 Term 2 23/1/12. Arrays You can create an array with the Array() constructor and the new operator. There are three ways to invoke the Array()

Similar presentations


Presentation on theme: "Lecture 3 Term 2 23/1/12. Arrays You can create an array with the Array() constructor and the new operator. There are three ways to invoke the Array()"— Presentation transcript:

1 Lecture 3 Term 2 23/1/12

2 Arrays You can create an array with the Array() constructor and the new operator. There are three ways to invoke the Array() constructor… 1.Call it with no arguments: var a = new Array(); 2

3 Arrays 2. Call it and explicitly specifying values for the first n elements of the array: var a = new Array(5, 6, 7, “a string”); Note: elements are assigned to the array starting with the element Specify a single numeric argument which specifies the length of the array var a = new Array(10); Note: this way overrides the second syntax. 3

4 Arrays You can access elements of an array using the [ ] operator. A reference to the array should appear to the left of the brackets An expression which is a non-negative integer should be inside the brackets. This syntax can be used to both read and write the value of an element of an array, for example… a[1] = 3.14; a[i] = 3; a[i + 1] = “hello”; 4

5 Array1 var book = new Array(4); book[0] = "Harry Potter"; book[1] = "Da Vinci Code"; book[2] = "The Kite Runner"; book[3] = "The Client"; for (i=0; i<4; i++) { document.write(book[i] + " "); } 5

6 Array2 var book = new Array("Harry Potter","Da Vinci Code","The Kite Runner","The Client"); for (i=0; i<4; i++) { document.write(book[i] + " "); } 6

7 Arrays Continued… In JavaScipt, as in Java, C, and C++ the first element of an array is at index 0. In JavaScript, unlike Java and C, an array can have any number of elements and the number of elements can be changed at any time. To add a new value to an array, simply assign a value to it a[10] =10; 7

8 Arrays have several methods associated with them: Array.length You can use the array.length property to return the number of elements in the array Array.join() Converts all the elements of an array to a string and concatenates them. Array.reverse() Reverses the order of the elements of an array. 8

9 var fruit = new Array("Apple","Orange","Pear","Strawberry"); for (i=0; i

10 Array.Reverse() Array1 var book = new Array(4); book[0] = "Harry Potter"; book[1] = "Da Vinci Code"; book[2] = "The Kite Runner"; book[3] = "The Client"; document.write(book.reverse()); 10

11 Arrays Array.slice() Returns a slice (or subarray) of the specified array. Its two arguments specify the start and end of the slice to be returned. Note the returned slice doesn’t include the element specified by the second argument. Array.toString() Outputs a comma delimited string of its elements 11

12 Array.Slice() Array1 var book = new Array(4); book[0] = "Harry Potter"; book[1] = "Da Vinci Code"; book[2] = "The Kite Runner"; book[3] = "The Client"; document.write(book.slice(0,2)); Output - ? 12

13 Array.sort() Sorts the elements of an array. This is done in place (i.e. without creating a new array). Array.concat() Creates and returns an array that contains the elements of the original array that concat() was invoked on, followed by each of the arguments to concat() 13 More Methods

14 Array.sort() Array1 var book = new Array(4); book[0] = "Harry Potter"; book[1] = "Da Vinci Code"; book[2] = "The Kite Runner"; book[3] = "The Client"; document.write(book.sort()); 14

15 Array.Concat() Array1 var book = new Array(4); book[0] = "Harry Potter"; book[1] = "Da Vinci Code"; book[2] = "The Kite Runner"; book[3] = "The Client"; var dvd = new Array(3); dvd[0] = "Mamma Mia"; dvd[1] = "The Usual Suspects"; dvd[2] = "The Grinch"; document.write(book.concat(dvd)); 15

16 Another Example var i; var schedule = new Array(); schedule[0] = "IS6116"; schedule[1] = "IS6117"; schedule[2] = "IS6118"; for (i in schedule) { document.write(schedule[i] + " "); }


Download ppt "Lecture 3 Term 2 23/1/12. Arrays You can create an array with the Array() constructor and the new operator. There are three ways to invoke the Array()"

Similar presentations


Ads by Google