220 likes | 529 Views
Alice in Action with Java. Chapter 5 Lists and Arrays. Objectives. Use a list to store multiple items Use Alice’s forAllInOrder and forAllTogether statements Use random numbers to vary the behavior of a program Use an array to store multiple items. Lists and Arrays.
E N D
Alice in Action with Java Chapter 5 Lists and Arrays
Objectives • Use a list to store multiple items • Use Alice’s forAllInOrderand forAllTogetherstatements • Use random numbers to vary the behavior of a program • Use an array to store multiple items Alice in Action with Java
Lists and Arrays • Some variables only store a single value • Number, Boolean, String, Object, Sound, Color • Variables that can store multiple values • list: container that can dynamically grow and shrink • array: container whose size remains fixed at run-time • Benefits of using variables that store multiple values • Reduces amount of code that needs to be written • Makes a program more readable • Example: a variable playList that holds 12 songs • Pass all 12 songs to a method using one parameter Alice in Action with Java
Lists and Arrays (continued) Alice in Action with Java
List Example 1: Flight of the Bumble Bees • Scene 2: a dozen bees rise to defend queen’s honor • Set up the scene by adding 12 bees and 1 queenBee • Manipulating 12 individual bees leads to inefficiencies • Adding 12 bees to a list simplifies implementation • Form: Object[]bees = bee, bee2,... bee12; • Defining bees list variable in playScene2() • Open Create New Local Variable dialog box • Select Object type and then make a List • Click new item and add the first bee to the list • Continue adding new items until the list holds 12 bees Alice in Action with Java
List of Bumble Bees Alice in Action with Java
Processing a List of Bumble Bees • Processing list entries • Drag the forAllInOrder statement to editing area • Drop forAllInOrder, select expressionsbees • Construct move()using bee2 as a placeholder • Drag item_from_beesonto the placeholder and drop • Send turnToFace()message to the queenBee • Send say()message with command to queenBee • Simulated behavior • queenBeeturns to each beeand orders it to rise • Each beeresponds by rising upward 5 meters Alice in Action with Java
List Operations • forAllInOrder statement • Used to sequentially perform actions on list items • forAllTogether statement • Used to simultaneously perform actions on list items • Example: cause all bee objects to take off at once • Each item in list is accessed by index (position) • Messages can be sent to an entire list or to list items • Example 1: aList.clear();(removes all items) • Example 2: aList.add(0, 1);(inserts 1 at list head) Alice in Action with Java
doAllInOrder Alice in Action with Java
doAllTogether Alice in Action with Java
List Operations Alice in Action with Java
List Operations • A few list functions • aList.size(): the number of items in aList • aList[i]:the value item iin aList • aList.getRandomItem(): random value in aList • Using list functions • Drag a list definition into the editing area • Drop list onto placeholder with function’s return type • Alice will display a list of messages to choose from Alice in Action with Java
List Example 2: Buying Tickets • Scene 3: line of people waiting to buy tickets • Animating the scene using the list data structure • Create a new world with a ticket box office • Line up five people in front of the ticket window • Add five people to the personList • Use nested looping to advance the line • After an outer loop iteration, the line is reduced by one • Testing the animation • Compare positions of each person after each iteration Alice in Action with Java
List Example 2: Buying Tickets (continued) Alice in Action with Java
The Array Structure • Fixed-size container that can store a group of values • Data items are accessed by position (as in a list) • Values of data items can be changed at run-time • The capacity of an array cannot change at run-time • Two reasons for using an array instead of a list • The array is more memory-efficient than a list • An array is more time-efficient for accessing an item Alice in Action with Java
Array Example 1: The Ants Go Marching • User story: soldier ant sings a song while marching • View the story as a counting problem • The ant song is 10 verses long • Most of the lines are repeated • A for loop can be used to iterate through 10 verses • Managing the differences in lines using parallel arrays • One String array stores the verse-specific numbers • Another String array stores verse-specific ant actions • Values at ithindex of array are accessed with each loop • Enhance the program by adding a marching method Alice in Action with Java
Array Example 1: The Ants Go Marching Alice in Action with Java
Array Operations • Array items are accessed by index (like lists) • Predefined operations for arrays • anArray[i] = val; (the subscript operation) • val.set(value, anArray[i]); • anArray.length • Two versions of the subscript operation • Write version: changes value of item i in the array • Read version: retrieves the value of item i in the array • Traverse an array using a for loop or while loop • forAllInOrder and forAllTogethernot available Alice in Action with Java
Array Example 2: Random Access • Scene: left castle door tells jokes to right castle door • Elements used to program random knock-knock jokes • Castle interior with camera positioned before doors • Parallel arrays for accessing name and punchline • Dialog including name and punchline arrays • Use of Random.nextDouble()to generate index • Random.nextDouble()details • Accessed in the functions pane of world’s details area • Set integerOnly attribute to true • Define a range of random values for indexing the array Alice in Action with Java
Array Example 2: Random Access (continued) Alice in Action with Java
Summary • Data structure: structure storing a group of values • List: dynamic container that holds a group of values • Array: static container that holds a group of values • Item: individual variable stored by index in an array or list • forAllInOrder statement: loops through list items in sequence Alice in Action with Java
Summary (continued) • forAllTogether statement: applies instructions to list items in parallel • Random.nextDouble():generates random numbers • Alice usually displays <None>to indicate that an object value is null Alice in Action with Java