Upload
derek-mccormick
View
36
Download
0
Embed Size (px)
DESCRIPTION
Scripting for Interactive Media. Declaring and Using Arrays. Agenda. Why Use an Array Array Declaration in ActionScript Searching and Sorting an Array The for - next loop Parallel Arrays Array Access Errors Tutorial Example AFI Movies. Need to Store Five Numbers. - PowerPoint PPT Presentation
Citation preview
UFCFY5-30-1Multimedia Studio
Scripting for Interactive Media
Declaring and Using Arrays
UFCFY5-30-1Multimedia Studio
Agenda
Why Use an Array Array Declaration in Java Searching and Sorting an Array Using a for - next loop with Arrays Parallel Arrays Array Access Errors
UFCFY5-30-1Multimedia Studio
Need to Store Five Numbers
Use individual variables for each number stored:
int numberOne;int numberTwo;int numberThree;int numberFour;int numberFive;
messy - have to process as individual items ratherthan as a collection of grouped items. Solution - use an Array - contiguous data storage
UFCFY5-30-1Multimedia Studio
Arrays and Data Storage
15
95
14
70
23
Elements
The value stored at array element number 2 is 14
Arrays store values in ‘elements’ (compartments)
i.e. contiguous memory locations
0
3
2
4
1
UFCFY5-30-1Multimedia Studio
Arrays and Data Storage
15
95
14
70
23
Elements
The array has 5 elements 0..4
The value stored at array element number 4 is 23
Array element numbering starts at 0
0
3
2
4
1
Thus the size of the array is 5
UFCFY5-30-1Multimedia Studio
Arrays and Data Types
15
23
14
70
5
An Array ofnumbers (integers)
Gibson
Fender
Aria
Martin
Array of stringsLes Paul
UFCFY5-30-1Multimedia Studio
Declaring Arrays in Java
// create a new array
[] numberArray = new int[5];
// store the numbers into the array// 15 95 14 70 23
numberArray[0] = 15;numberArray[1] = 95;numberArray[2] = 14;numberArray[3] = 70;numberArray[4] = 23;
UFCFY5-30-1Multimedia Studio
The for – next Loop
// loops for a set number of times
for (start; condition; increment) {
// do something here
}
UFCFY5-30-1Multimedia Studio
Accessing Array Elements
// iterate through the array
for (int index = 0; index <= 4; index++) {System.out.println(numberArray[index]);
}
// outputs each number in the Array
UFCFY5-30-1Multimedia Studio
System Output from for loop
1595147023
UFCFY5-30-1Multimedia Studio
Adding Array Elements
// example of declaring specific(literal) values for an array
int [] numberValues = {2, 5, 23, 37, 11, 19, 49, 6, 15, 41} ;
int total = 0;
for (int index = 0; index <= 9; index++){
total = total + numberValues[index];
}
System.out.println("The total of all the numbers is:" + total);
UFCFY5-30-1Multimedia Studio
Summing the Values of an Array 2
// total the numbers stored in the number array using array// length method
int index;// loop control variable int sum = 0;
for (index = 0; index <= numberArray.length; index++){sum = sum + numberArray[index]
}
System.out.printl("The sum of the numbers in the array is " + sum)
UFCFY5-30-1Multimedia Studio
Declaring String Arrays in Java
// create a new array
String [] nameArray = new String[5];
// store the names into the array
nameArray[0] = “Bugs Bunny”;nameArray[1] = “Daffy Duck”;nameArray[2] = “Yosemite Sam”;nameArray[3] = “Mickey Mouse”;nameArray[4] = “Porky Pig”;
UFCFY5-30-1Multimedia Studio
for..next loop to output contents
for (int index = 0; index <= 4; index++){
System.out.println(nameArray[index]);
}
UFCFY5-30-1Multimedia Studio
System Output
Bugs Bunny
Daffy Duck
Yosemite Sam
Mickey Mouse
Porky Pig
UFCFY5-30-1Multimedia Studio
for..next loop to search for a match
for (int index = 0; index <=4; index++){
if(nameArray[index] == "Mickey Mouse"){
System.out.println("Walt Disney Studios :" + nameArray[index]);
} // end if
else {
System.out.println("Warner Bros. Pictures :" + nameArray[index]);
}// end else
} // end for
UFCFY5-30-1Multimedia Studio
System Output from Search
Warner Bros. Pictures :Bugs Bunny
Warner Bros. Pictures :Daffy Duck
Warner Bros. Pictures :Yosemite Sam
Walt Disney Studios :Mickey Mouse
Warner Bros. Pictures :Porky Pig
UFCFY5-30-1Multimedia Studio
Typical Aspects of using Arrays
• Search an array for a given value
• Sort an array in a given order
• Find the size of the array
• Use the length method in searching algorithms
• Compare the contents of one array with another array
UFCFY5-30-1Multimedia Studio
Beware! The classic runtime error is where a program tries to access an array element beyond the array’s size.
for (index = 0; index <=5; index++)The array has only five elements, but the code is attempting to access element number six (5), which does not exist.
The computer speak is the code has ‘fallen off the end of the array’
UFCFY5-30-1Multimedia Studio
Parallel Arrays
name
John
Ben
Freda
Sue
Mike
UFCFY5-30-1Multimedia Studio
Parallel Array Search
name
John
Ben
Freda
Sue
Mike
Search on name
Extract email on name
UFCFY5-30-1Multimedia Studio
Parallel Array Search
name
John
Ben
Freda
Sue
Mike
Search on name
Find occurrences on name of email
UFCFY5-30-1Multimedia Studio
Arrays of Data (sounds)
Array of .mp3 audio files
footsteps.mp3
shell.mp3
siren.mp3
alarm.mp3
ambience.mp3
UFCFY5-30-1Multimedia Studio
Summary
• Arrays are a fundamental structured data type in any programming language
• Arrays store values usually of the same type in a contiguous way which allows the data to be accessed and manipulated as a collection of data
• Typical array access is to search and sort the contents
• Associated data may be held in parallel arrays and use cross referencing to extract the required information
• Algorithms that manipulate arrays must stay within the array bounds or runtime errors will occur causing the program to ‘freeze’ or ‘crash’.
UFCFY5-30-1Multimedia Studio
Revise Last Week’s Concepts
Three new coding structures and one new variable type
UFCFY5-30-1Multimedia Studio
The Lottery Draw
How will this week’s new concepts help to code a solution to the Lottery Draw scenario?
UFCFY5-30-1Multimedia Studio
Tutorial Example AFI Movies
The first of the American Film Institute (AFI)
100 Years series of cinematic milestones.
AFI's 100 Years…100 films is a list of
the 100 best American films, as determined by the
American Film Institute from a poll of more than 1,500
artists and leaders in the film industry who chose from
a list of 400 nominated films.
The 100-best list was unveiled in 1998.
UFCFY5-30-1Multimedia Studio
Parallel Arrays filmList and userRating
filmList
7
3
9
8
6
userRating
Citizen Kane
Casablanca
The Godfather
Gone with the Wind
Lawrence of Arabia
UFCFY5-30-1Multimedia Studio
Parallel Arrays filmList and userRating
7
3
9
8
6
Citizen Kane
Casablanca
The Godfather
Gone with the Wind
Lawrence of Arabia
element 0 not used
1
2
3
4
5
UFCFY5-30-1Multimedia Studio
var filmList:Array = new Array();
filmList[1] = "Citizen Kane";
filmList[2] = "Casablanca";
filmList[3] = "The Godfather";
filmList[4] = "Gone with the Wind";
filmList[5] = "Lawrence of Arabia";
.
.
filmList[99]= "Guess Who's Coming to Dinner";
filmList[100]= "Yankee Doodle Dandy";
AFI Movies Array- filmList
UFCFY5-30-1Multimedia Studio
var userRating:Array = new Array();
userRating[1] = 7;
userRating[2] = 6;
userRating[3] = 8;
userRating[4] = 3;
userRating[5] = 9;
.
.
userRating[99] = 2;
userRating[100] = 7;
AFI Movies Array- userRating
UFCFY5-30-1Multimedia Studio
var index:int // loop variable for the for loop index
// two functions to initialize the data
setFilms();// initializes the film list
setRating(); initializes the user-ratings
Index Loop Variable and Array Initialization
UFCFY5-30-1Multimedia Studio
// output all the films in the filmList array
for(index=1; index <= 100; index++){
trace(filmList[index]);
}
Output all the films in the filmList Array
UFCFY5-30-1Multimedia Studio
// output all the films in the array
// and show the user rating
for(index=1; index <= 100; index++){
trace(filmList[index] + " " + userRating[index]);
}
}
Output all the films and show the user rating
UFCFY5-30-1Multimedia Studio
// output all the films where userRating > 7
for(index=1; index <= 100; index++){
if(userRating[index] > 7){
trace(filmList[index] + " " + userRating[index]);
}
}
Output all the films with a user rating greater than 7
UFCFY5-30-1Multimedia Studio
// output all the films where userRating = 5
for(index=1; index <= 100; index++){
if(userRating[index] == 5){
trace(filmList[index] + " " + userRating[index]);
}
}
Output all the films with a user-rating of 5
UFCFY5-30-1Multimedia Studio
correct
if(userRating[index] == 5)comparison
incorrect
if(userRating[index] = 5)assignment
this will output all the movies!
Watch Out for a Logical Error!
UFCFY5-30-1Multimedia Studio
// use the these lecture slides to carry out the exercises
// e.g. output all the films in the filmList array
for(index=1; index <= 100; index++){
// put your code here
}
Tutorial with Working Code for you to Modify