The Anatomy of an Array

Each item stored in an array is called an array element, and each has a unique number (index) by which we can refer to it.

Array Elements

Like a variable, each array element can store any legal datum. An entire array, then, is akin to a collection of sequentially named variables, but instead of each item having a different name, it has an element number (the first element is number 0, not number 1).

Figure 11.1 shows, conceptually, the structure of an array that contains three elements. Element stores the value “Erica”, element 1 stores the value “Slavik”, and element 2 stores the value “Gary”.

A sample array structure

Figure 11-1. A sample array structure

To manipulate the values in an array’s elements, we ask for them by number. In our chest of drawers analogy we might ask ActionScript to store something in the first drawer or retrieve whatever is in the second drawer for us.

Array Element Indexing

An element’s position in the array is known as its index. Just as we can access the seventh character in a string, we can access the seventh element of an array via its index (in this case, the index is 6). We use an element’s index to set or retrieve the element’s value or to work with the element in various other ways. Some of the array-handling functions, for example, use element indexes to specify ranges of elements for processing.

We can also insert and delete elements from the beginning, ...

Get ActionScript: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.