D.A. Cle me nts Indexing a Collection of Items Arrays: Arrays: FIT100 FIT100 FIT100
Just a thought… FIT100 FIT100 FIT100
Arrays FIT100 FIT100 FIT100 • I nde xing ∗ Cre a ting a nd using lists, o r a rra ys Cre a ting a nd using lists, o r a rra ys • Pro c e ssing a n a rra y ∗ E ∗ E le me nt b y e le me nt le me nt b y e le me nt • Arra y me tho ds ∗ Quic k wo rk with lists Q i k k ith li t
Cre a ting a nd using lists, o r a rra ys XING XING INDE INDE FIT100 FIT100 FIT100
What is an Array? • An inde xe d list o f ite ms, o r e le me nts FIT100 FIT100 FIT100 • I nde xe d me a ns e a c h e le me nt in the list ha s a numb e r o r inde x numb e r, o r inde x 1. Ge o rg e Wa shing to n 14. Ja me s Buc ha na n 28. Wa rre n Ha rding 2. 2 Jo hn Ada ms Jo hn Ada ms 15. Ab ra ha m L 15 Ab ra ha m L inc o ln inc o ln 29 Ca lvin Co o lidg e 29. Ca lvin Co o lidg e 3. T ho ma s Je ffe rso n 16. Andre w Jo hnso n 30. He rb e rt Ho o ve r 31. F ra nklin D. Ro o se ve lt 4. Ja me s Ma diso n 17. Ulysse s S. Gra nt 32. Ha rry S. T ruma n 5. Ja me s Mo nro e 18. Ruthe rfo rd B Ha ye s 33. Dwig ht E ise nho we r 6. Jo hn Quinc y Ada ms 19. Ja me s Ga rfie ld 34. Jo hn K e nne dy 7. Andre w Ja c kso n 20. Che ste r Arthur 35. L yndo n Jo hnso n 8. Ma rtin Va n Bure n 21. Gro ve r Cle ve la nd 36. Ric ha rd Nixo n 36 Ri h d Ni 9. 9 Willia m Ha rriso n Willi H i 22 B 22. Be nja min Ha rriso n j i H i 37. Ge ra ld F o rd 10. Jo hn T yle r 23. Gro ve r Cle ve la nd 38. Ja me s Ca rte r 11. Ja me s Po lk 24. Willia m Mc K inle y 39 Ro na ld Re a g a n 39. Ro na ld Re a g a n 12. Za c ha ry T 12 Za c ha ry T a ylo r a ylo r 25. T 25 T he o do re he o do re Ro o se ve lt 40. Ge o rg e H. W. Bush 13. Milla rd F illmo re 26. Willia m H. T a ft 41. Willia m Clinto n 14. Franklin Pierce 27. Wo o dro w Wilso n 42. Ge o rg e W. Bush
Indexing FIT100 FIT100 FIT100 • Pro c e ss o f c re a ting a se q ue nc e o f na me s b y a sso c ia ting a b a se na me with a numb e r (like Apo llo 13 o r He nry VI I I ) ∗ E a c h inde xe d ite m is c a lle d a n e le me nt o f the b a se - na me d se q ue nc e • I • I nde x Synta x nde x Synta x ∗ inde x numb e r is e nc lo se d in sq ua re b ra c ke ts [ ] • I I te ra tio ns c a n b e use d to re fe r to a ll e le me nts o f te ra tio ns c a n b e use d to re fe r to a ll e le me nts o f a na me ∗ A[j] fo r suc c e ssive ite ra tio ns o ve r j re fe rring to diffe re nt e le me nts o f A diffe re nt e le me nts o f A
Indexing (cont'd) FIT100 FIT100 FIT100 • I nde x Orig in ∗ T he po int a t whic h inde xing b e g ins p g g (the le a st inde x) ∗ I I n life , the first e le me nt ma y b e g in with n life , the first e le me nt ma y b e g in with 1, o r ha ve no numb e r (Que e n E liza b e th) ∗ Ja va Sc ript always use s inde x o rig in 0
Rules for Arrays FIT100 FIT100 FIT100 • Arra ys a re va ria b le s initia lize d b y • Arra ys a re va ria b le s initia lize d b y new Array (<number of elements>); • <numb e r o f e le me nts> is numb e r o f ite ms in • <numb e r o f e le me nts> is numb e r o f ite ms in ; • Arra y inde xing b e g ins a t 0 Arra y inde xing b e g ins a t 0 • Gre a te st inde x is <numb e r o f e le me nts> - 1 • Numb e r o f e le me nts is a rra y le ng th • I I nde x va lue s ra ng e fro m 0 to (le ng th – 1) nde x va lue s ra ng e fro m 0 to (le ng th 1)
Array Reference Syntax FIT100 FIT100 FIT100 • Arra y re fe re nc e is a rra y na me to g e the r with inde x e nc lo se d in b ra c ke ts (no n-ne g a tive inte g e r o r e xpre ssio n o r va ria b le tha t re so lve s to no n-ne g a tive inte g e r) array[i] y[ ]
Array Reference Syntax FIT100 FIT100 FIT100 • T he Wo rld-F a mo us I te ra tio n, o r 0-o rig in lo o p ite ra tio n, is pe rfe c t fo r lo o ping thro ug h a rra ys ∗ Sta rt a t 0 ∗ I nc re me nt b y 1 to pro c e ss e ve ry e le me nt in the a rra y • Use the inc re me nting va ria b le a s the inde x fo r the a rra y e le me nt ∗ E ∗ E nd whe n yo u re a c h the la st e le me nt in the nd whe n yo u re a c h the la st e le me nt in the a rra y
AY AY R R SSING AN AR SSING AN AR le me nt b y e le me nt OCE OCE FIT100 FIT100 FIT100 PR PR E
for Loops Rule L R l f FIT100 FIT100 FIT100 • T he Wo rld-F a mo us I te ra tio n fo r lo o ping thro ug h a n a rra y: g y for ( i = 0; i < fruits.length; i++ ) { alert(fruits[i]); } • .length is a b uilt-in Ja va Sc ript pro pe rty tha t a lwa ys g ive s yo u the le ng th o f a n a rra y i f ∗ Length of an array is the number of elements
Demonstration FIT100 FIT100 FIT100 • Looping through the fruits array
Processing elements in an Processing elements in an array FIT100 FIT100 FIT100 var i, text=""; //declare iteration and other variables var fruits = new Array( 'lemons','apples','mangoes','tangerines','kumquats', 'l ' ' l ' ' ' 't i ' 'k t ' 'cantaloupe','peaches','grapefruit','raspberries'); alert("Total number of fruits is " + fruits.length); for (i=0; i<fruits.length; i++) { text += i + '. ' + fruits[i] + '<br />'; [ ] ; } document.write("<h1>Elements of Fruits Array:</h1><p>" + text + "</p>"); text + </p> );
Arra y Me tho ds: .push A M th d FIT100 FIT100 FIT100 • Add mo re fruits to the a rra y with .push var i, text=""; //declare iteration and other variables , ; var fruits = new Array( 'lemons','apples','mangoes','tangerines','kumquats','cantaloupe', 'peaches' 'grapefruit' 'raspberries'); peaches , grapefruit , raspberries ); fruits. push('bananas','oranges','pears'); alert("Total number of fruits is " + fruits.length); f for (i=0; i<fruits.length; i++) (i 0 i<f it l th i++) { text += i + '. ' + fruits[i] + '<br />'; } document.write("<h1>Elements of Fruits Array:</h1><p>" + text + "</p>");
for Loops Rule! • Afte r a dding mo re e le me nts to o ur FIT100 FIT100 FIT100 a rra y do e s o ur fo r lo o p still wo rk? a rra y, do e s o ur fo r lo o p still wo rk? for ( i = 0; i < fruits.length; i++ ) { alert(fruits[i]); } • Ye s! fruits.length still ta ke s us to the e nd o f the fruits a rra y— the e nd o f the fruits a rra y wha te ve r its le ng th.
Array Methods: push A M h d FIT100 FIT100 FIT100 • Verify it by looping through the expanded fruits array p y
HODS HODS T T AY ME AY ME Quic k wo rk with lists R R FIT100 FIT100 FIT100 AR AR
Array Methods = Possibilities! FIT100 FIT100 FIT100 • push push ∗ a dds e le me nts to the a rra y fruits push(‘ b a na na s’ ’ ne c ta rine s’ ’ a pple s’ ); fruits.push( b a na na s , ne c ta rine s , a pple s ); • pop ∗ pulls the la st e le me nt o ff o f the a rra y ∗ pulls the la st e le me nt o ff o f the a rra y fruits.po p(); • c onc at c onc at ∗ c o mb ine s se ve ra l a rra ys into o ne ∗ No te : c o pie s o f the a rra ys a re use d ∗ T he o rig ina l a rra ys re ma in a nd a re una ffe c te d fruits.c o nc a t(c itrus,sto ne F ruit,b e rrie s);
Array Methods = Possibilities! y FIT100 FIT100 FIT100 • join join ∗ c o mb ine s a ll e le me nts into a string , se pa ra te d b y c o mma s o r a s spe c ifie d: fruits.jo in(;); c o mma s o r a s spe c ifie d: fruits.jo in(;); • sor t ∗ so rts the e le me nts in the a rra y so s e e e e s e a a y fruits.so rt(); / / a lwa ys a sc e nding • r e ve r se ∗ re ve rse s the e le me nts in a n a rra y ∗ Use d with so rt to so rt de sc e nding fruits.so rt(); / / so rts into a sc e nding o rde r fruits.re ve rse (); / / re ve rse s to de sc e nding
Array Methods = Possibilities! FIT100 FIT100 FIT100 • toStr ing ∗ c o nve rts the a rra y to a string fruits.string ();
Array Method: sort y FIT100 FIT100 FIT100 • So rt with .sort ∗ Asc e nding o nly (A-Z, 0-9) var i, text=""; //declare iteration and other variables var fruits = new Array( 'lemons','apples','mangoes','tangerines','kumquats','cantaloupe', 'peaches','grapefruit','raspberries'); fruits. push('bananas','oranges','pears'); fruits.sort(); alert("Total number of fruits is " + fruits.length); alert( Total number of fruits is fruits.length); for (i=0; i<fruits.length; i++) { text += i + ' ' + fruits[i] + '<br />'; text += i + . + fruits[i] + <br /> ; } document.write("<h1>Elements of Fruits Array:</h1><p>" + text + "</p>");
Array Sort • De mo nstra tio n FIT100 FIT100 FIT100
Recommend
More recommend