1Lists and Arrays2/11/20082Opening Discussion■Let's look at solutions to the interclass problem.■Minute Essays3Problem■I want to be able to do our tag game with more objects and I don't want to have to repeat writing the same code over and over again.■I could also want the mummy to go and attack many other objects in order of proximity and not have to repeat a lot of code.4Working with Multiple Objects■While we have given ourselves a lot of power in Alice, there are still some things that are difficult. Working with a large number of different objects is an example.■To do this we need some construct in the language that represents a group of things so that we can manipulate the group or give commands to elements in the group.■In Alice we accomplish this with lists and arrays.5Lists in Alice■A list is a type of abstract data type with certain operations defined on it that mimic what we generally think of as things we can do to a list.■You use a list in Alice when you have a collection of objects that you want to add to and remove from.■The dialog box that comes up for variables, parameters, properties, and function return types has an option that allows you to make it into a list. Lists can hold any of the Alice types in them.6List Operations■Alice provides the operations forAllInOrder and forAllTogether to allow you to do something to all of the elements of a list.■The loop will create a variable that stores each item from the list and executes the code in the loop body.■At this point we need to make a list and demonstrate using each of these constructs on the items of the list.7List Methods■Lists have methods in them that we can call to modify the contents of the list.Three versions of add put new items in the list as different locations.Versions of remove take something out of the list.The clear method takes everything out of the list.■Note that the index of a list starts at 0, not 1. This is common in many modern programming languages. So the add method with a first argument of 0 places a new item at the beginning of the list.8List Functions■Lists also have functions that we can call to get information from the list.size tells us how many elements are on the list.firstIndexOf searches for something in the list and gives the index it first appears at.lastIndexOf is similar to firstIndexOf, but starts searching at the end.[] get an item from the list.getLastItem and getRandomItems return the respective items.9Arrays■Arrays are much like lists and in most languages are the most commonly used way of dealing with many things.■Unlike lists, the size of an array doesn't change after it is created. It has a fixed number of slots. You can set elements, but you can't insert and you don't really have the option to remove.■Unfortunately, arrays don't work well in the current version of Alice so we will pretty much ignore them and use lists.10Coding■Let's actually do something with a list in our program.11Minute Essay■How might you use a list in your project?■Remember to turn in your project ideas.■Interclass Problem - Place four objects in a world. Three should be characters you move around and one should be a stationary object. Put the three characters on a list and have them move to the stationary object sliding one meter at a time. Each should stop when it gets close to the
View Full Document