9: Arrays and iteration - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

9: Arrays and iteration

Description:

Possibly different types. Accessed by name. Array. Collection of things, (same type) ... Array('Eat', 'apples', 'daily', 'get', 'big', 'Ears') Displaying arrays ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 16
Provided by: markels
Category:
Tags: apples | arrays | iteration | of | types

less

Transcript and Presenter's Notes

Title: 9: Arrays and iteration


1
9 Arrays and iteration
  • Mark Elsom-Cook 1999

2
Contents
  • Collections of data
  • Accessing arrays
  • When to use arrays
  • Recursion on arrays
  • Iteration on arrays

3
Collections of data
  • Variable a thing
  • (e.g. integer, string)
  • Object a group
  • Related things
  • Possibly different types
  • Accessed by name
  • Array
  • Collection of things, (same type)
  • Accessed by index (number)

4
Declaring arrays
  • Need to name it
  • Instance of array object
  • allocates memory
  • Normally(!) set size
  • but not in JavaScript
  • var pig new Array()

5
Accessing arrays
  • Array has an index range
  • From 0 to ??
  • pig.length
  • Dynamic in JavaScript
  • Out of range error
  • This is square brackets!
  • pig0Hello Mark,
    pig27Thursday
  • document.writeln(pig27)

6
Arrays - a picture
  • var pignew Array()
  • Array extends to size of assignment
  • Access above assignment is out-of-range error
  • Access of unassigned is undefined error

7
When to use arrays
  • Lots of things of the same sort, OR
  • ...you dont know how many there are, OR
  • they are in some sort of order, OR
  • ...you want to do something to all of them

8
Three ways to make arrays
  • Always use new to allocate memory
  • var freeble new Array( )
  • no arguments creates an array - Array( )
  • one argument creates an array of a size
  • Array(15)
  • many arguments sets initial values
  • Array(Eat, apples, daily, get, big,
    Ears)

9
Displaying arrays with index
  • Var frood new Array(Before, the, wire)
  • document.writeln(1 is frood0 )
  • document.writeln(2 is frood1 )
  • document.writeln(3 is frood2 )
  • document.writeln(4 is frood3 )
  • document.writeln(5 is frood4 )
  • document.writeln( There are frood.length
    items)

10
Recursion on arrays
  • Call with an array showArray(frood)
  • Write it like this
  • function showArray(arr)
  • showallArr(arr, arr.length, 0)
  • function showAllArr(a, alen, count)
  • if (count gt alen) document.writeln(Finished)
  • else
  • document.writeln(count acount ltbrgt)
  • showAllArr(a, alen, count1)

11
Iteration
  • Iteration does the same thing as recursion
  • i.e. execute things repeatedly
  • More commonly used
  • Sometimes faster and more concise
  • Various forms
  • e.g. for, while, repeat

12
The for statement
  • for ( start stop change)
  • things to do

13
A for example
  • for (var x1 xlt10x)
  • document.writeln(x (xx))

14
Iteration on arrays
  • function showArray(arr)
  • for (var j0 j lt arr.length j)
  • document.writeln(j arrj)

15
Summary
  • Whats an array?
  • Why use one?
  • How to access them
  • What is iteration?
  • Using iteration with arrays
Write a Comment
User Comments (0)
About PowerShow.com