Arrays

An array is a thing that holds collection of things, kept in order. Most often, it is used to keep a set of similar objects. For instance, here is a collection of fruits: [|(photo credits -] all hosted on flickr.com)

The array is actually the box and numbers - the fruits themselves are the things we put //into// the array. Each box is automatically numbered, always starting with zero. So we would say that item # 3 in this array is a banana.

Here's how this might look in code: code format="actionscript3" {  var fruitList:Array; fruitList = new Array; fruitList.push(pineapplePicture); fruitList.push(cherryPicture); fruitList.push(pearPicture); fruitList.push(bananaPicture); fruitList.push(lemonPicture); fruitList.push(apricotPicture); } code Notice that we start by saying that fruitList is the kind of variable that can be an Array. We then create a new Array, and assign it to fruitList. The array starts off empty - that is, it has zero items. To put items into the array, we "push" them, one at a time.

We can access this list several ways. Suppose these pictures were actually movieclips. We could make the lemon appear by saying code format="actionscript3" addChild(fruitList[4]); code Or, if we wanted to replace the cherry with a blueberry, we might say: code format="actionscript3" fruitList[1] = strawberryPicture; code In either case, we are using the name of the array, followed by a number contained in square brackets ( "[" "]"). The number must be no less than zero, and no more than the size of the array minus one. (Notice that there are six fruits in the list right now - but the list only goes up to five.

In the two examples above, using the square brackets does not change the number of items in the list. In the second example, for instance, blueberryPicture isn't just added to the list - it takes cherryPicture's place.

**Removing items from an array**
If you do wish to remove one or more items from the list, you can do so by one of three methods: 1) To remove the last item on the list, you can tell the Array to **pop**. This removes the last item, making the list get shorter, and returns what it was. For example, you might say: code format="actionscript3" removeChild(fruitList.pop); code This would remove the apricot from the list, and temporarily replace the "fruitList.pop" method with apricotPicture - thus the command would (temporarily) be two removeChild(apricotPicture); - and now the list would only have 5 items.

2) To remove the first item on the list, you can tell the Array to **shift**. Similar to pop, this will remove the first item in the list, making the list shorter, and return what it was. For example, you might say: code format="actionscript3" removeChild(fruitList.shift); code This would wind up removing the pineapple from both the list and from the screen. The secondary effect of this is that the item formerly at position 1 (the cherry or blueberry) is now shifted so that it is at position 0. Likewise, the pear at position 2 is now at position 1, and so forth, until the lemon is now pushed to position 3 and we only have 4 items left.

3) To remove one or more items from the middle of the list, we can use the **splice** command. Unlike the pop and shift commands, this does not return the items that we remove. We could say: code format="actionscript3" fruitList.splice(2,1); code ... and this would say to start at position 2 (now the banana) and remove one item. (Goodbye, banana!) The following fruit(s) would slide towards the front of the line. However, although this removes the banana from the list, we didn't write any code to remove it from the movie...

Finding the size of an Array There is an attribute built into Actionscript Arrays called **length**. It can be very handy. After all the removes and changes we have seen, the list only has the blueberry, the pear, and the lemon. If we said: code format="actionscript3" trace("There are "+fruitList.length+" items in the list."); code ... we would get the response: There are 3 items in the list.