FIT100 FIT100 FIT100
Arrays: Arrays:
Indexing a Collection of Items
D.A. Cle me nts
Arrays: Arrays: FIT100 FIT100 FIT100 Just a thought FIT100 - - PDF document
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
D.A. Cle me nts
Cre a ting a nd using lists, o r a rra ys
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
15 Ab ra ha m L inc o ln 1. Ge o rg e Wa shing to n 2 Jo hn Ada ms
29 Ca lvin Co o lidg e
inc o ln
2. Jo hn Ada ms 3. T ho ma s Je ffe rso n 4. Ja me s Ma diso n 5. Ja me s Mo nro e
ra nklin D. Ro o se ve lt
ruma n
22 B j i H i 6. Jo hn Quinc y Ada ms 7. Andre w Ja c kso n 8. Ma rtin Va n Bure n 9 Willi H i
ise nho we r
e nne dy
yndo n Jo hnso n 36 Ri h d Ni
inle y 25 T he o do re 9. Willia m Ha rriso n
yle r
12 Za c ha ry T a ylo r
39 Ro na ld Re a g a n
he o do re Ro o se ve lt
a ft
a ylo r
illmo re
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
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 [ ]
te ra tio ns c a n b e use d to re fe r to a ll e le me nts o f 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 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
new Array (<number of elements>);
nc re me nt b y 1 to pro c e ss e ve ry e le me nt in the a rra y
a rra y e le me nt
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
E le me nt b y e le me nt
var i, text=""; //declare iteration and other variables , ; var fruits = new Array( 'lemons','apples','mangoes','tangerines','kumquats','cantaloupe', 'peaches' 'grapefruit' 'raspberries'); peaches , grapefruit , raspberries );
alert("Total number of fruits is " + fruits.length); f (i 0 i<f it l th i++) for (i=0; i<fruits.length; i++) { text += i + '. ' + fruits[i] + '<br />'; } document.write("<h1>Elements of Fruits Array:</h1><p>" + text + "</p>");
Quic k wo rk with lists
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 );
fruits.po p();
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);
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(;);
so s e e e e s e a a y fruits.so rt(); / / a lwa ys a 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
fruits.string ();
var i, text=""; //declare iteration and other variables var fruits = new Array( 'lemons','apples','mangoes','tangerines','kumquats','cantaloupe', 'peaches','grapefruit','raspberries');
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>");
var i, text=""; //declare iteration and other variables var fruits = new Array( 'lemons','apples','mangoes','tangerines','kumquats','cantaloupe', 'peaches','grapefruit','raspberries');
fruits.sort(); fruits.reverse(); 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>");
So urc e : F re de ric k P. Bro o ks, Jr. T he Mythic al Man-Mo nth: E ssays o n S
E ng ine e ring