Arrays: Arrays: FIT100 FIT100 FIT100 Just a thought FIT100 - - PDF document

arrays arrays
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

FIT100 FIT100 FIT100

Arrays: Arrays:

Indexing a Collection of Items

D.A. Cle me nts

slide-2
SLIDE 2

FIT100 FIT100 FIT100

Just a thought…

slide-3
SLIDE 3

FIT100 FIT100 FIT100

Arrays

  • 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

le me nt b y e le me nt

∗ E

le me nt b y e le me nt

  • Arra y me tho ds

Q i k k ith li t

∗ Quic k wo rk with lists

slide-4
SLIDE 4

FIT100 FIT100 FIT100

INDE XING

Cre a ting a nd using lists, o r a rra ys

INDE XING

slide-5
SLIDE 5

What is an Array?

FIT100 FIT100 FIT100

  • An inde xe d list o f ite ms, o r e le me nts
  • 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

  • 14. Ja me s Buc ha na n

15 Ab ra ha m L inc o ln 1. Ge o rg e Wa shing to n 2 Jo hn Ada ms

  • 28. Wa rre n Ha rding

29 Ca lvin Co o lidg e

  • 15. Ab ra ha m L

inc o ln

  • 16. Andre w Jo hnso n
  • 17. Ulysse s S. Gra nt
  • 18. Ruthe rfo rd B Ha ye s

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

  • 29. Ca lvin Co o lidg e
  • 30. He rb e rt Ho o ve r
  • 31. F

ra nklin D. Ro o se ve lt

  • 32. Ha rry S. T

ruma n

  • 19. Ja me s Ga rfie ld
  • 20. Che ste r Arthur
  • 21. Gro ve r Cle ve la nd

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

  • 33. Dwig ht E

ise nho we r

  • 34. Jo hn K

e nne dy

  • 35. L

yndo n Jo hnso n 36 Ri h d Ni

  • 22. Be nja min Ha rriso n
  • 23. Gro ve r Cle ve la nd
  • 24. Willia m Mc K

inle y 25 T he o do re 9. Willia m Ha rriso n

  • 10. Jo hn T

yle r

  • 11. Ja me s Po lk

12 Za c ha ry T a ylo r

  • 36. Ric ha rd Nixo n
  • 37. Ge ra ld F
  • rd
  • 38. Ja me s Ca rte r

39 Ro na ld Re a g a n

  • 25. T

he o do re Ro o se ve lt

  • 26. Willia m H. T

a ft

  • 27. Wo o dro w Wilso n
  • 12. Za c ha ry T

a ylo r

  • 13. Milla rd F

illmo re

  • 14. Franklin Pierce
  • 39. Ro na ld Re a g a n
  • 40. Ge o rg e H. W. Bush
  • 41. Willia m Clinto n
  • 42. Ge o rg e W. Bush
slide-6
SLIDE 6

FIT100 FIT100 FIT100

Indexing

  • 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

nde x Synta x

  • I

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

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

slide-7
SLIDE 7

FIT100 FIT100 FIT100

Indexing (cont'd)

  • 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

n life , the first e le me nt ma y b e g in with I 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

slide-8
SLIDE 8

FIT100 FIT100 FIT100

  • Arra ys a re va ria b le s initia lize d b y

Rules for Arrays

  • 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

nde x va lue s ra ng e fro m 0 to (le ng th – 1) I nde x va lue s ra ng e fro m 0 to (le ng th 1)

slide-9
SLIDE 9

FIT100 FIT100 FIT100

Array Reference Syntax

  • 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[ ]

slide-10
SLIDE 10

FIT100 FIT100 FIT100

Array Reference Syntax

  • 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

nd whe n yo u re a c h the la st e le me nt in the

∗ E

nd whe n yo u re a c h the la st e le me nt in the a rra y

slide-11
SLIDE 11

FIT100 FIT100 FIT100

PR OCE SSING AN AR R AY

E le me nt b y e le me nt

PR OCE SSING AN AR R AY

slide-12
SLIDE 12

f L R l

FIT100 FIT100 FIT100

for Loops Rule

  • 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

i f tha t a lwa ys g ive s yo u the le ng th o f a n a rra y

∗ Length of an array is the number of elements

slide-13
SLIDE 13

FIT100 FIT100 FIT100

Demonstration

  • Looping through the fruits array
slide-14
SLIDE 14

Processing elements in an

FIT100 FIT100 FIT100

Processing elements in an array

var i, text=""; //declare iteration and other variables var fruits = new Array( 'l ' ' l ' ' ' 't i ' 'k t ' 'lemons','apples','mangoes','tangerines','kumquats', '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> );

slide-15
SLIDE 15

A M th d

FIT100 FIT100 FIT100

Arra y Me tho ds: .push

  • 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 (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>");

slide-16
SLIDE 16

for Loops Rule!

FIT100 FIT100 FIT100

  • Afte r a dding mo re e le me nts to o ur

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.

slide-17
SLIDE 17

A M h d

FIT100 FIT100 FIT100

Array Methods: push

  • Verify it by looping through the

expanded fruits array p y

slide-18
SLIDE 18

FIT100 FIT100 FIT100

AR R AY ME T HODS

Quic k wo rk with lists

AR R AY ME T HODS

slide-19
SLIDE 19

FIT100 FIT100 FIT100

Array Methods = Possibilities!

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);

slide-20
SLIDE 20

Array Methods = Possibilities!

FIT100 FIT100 FIT100

y

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

slide-21
SLIDE 21

FIT100 FIT100 FIT100

Array Methods = Possibilities!

  • toStr

ing

∗ c o nve rts the a rra y to a string

fruits.string ();

slide-22
SLIDE 22

Array Method: sort

FIT100 FIT100 FIT100

y

  • 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>");

slide-23
SLIDE 23

FIT100 FIT100 FIT100

Array Sort

  • De mo nstra tio n
slide-24
SLIDE 24

Sort in Descending Order

FIT100 FIT100 FIT100

Sort in Descending Order

  • Re ve rse the so rt with .reverse

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(); 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>");

slide-25
SLIDE 25

A M h d

FIT100 FIT100 FIT100

Array Method: reverse

  • De mo nstra tio n
slide-26
SLIDE 26

FIT100 FIT100 FIT100

End papers…

  • Why is pro g ra mming fun?
  • Se c o nd is the ple a sure o f ma king thing s

Se c o nd is the ple a sure o f ma king thing s tha t a re use ful to o the r pe o ple . De e p within, we wa nt o the rs to use o ur wo rk a nd to find it he lpful. I n this re spe c t the pro g ra mming syste m is no t e sse ntia lly diff t f th hild' fi t l il diffe re nt fro m the c hild's first c la y pe nc il ho lde r “fo r Da ddy's o ffic e .”

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

  • ftware

E ng ine e ring