Table of Contents

11.2 The Anatomy of an Array

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

11.2.1 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, each item 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 0 stores the value "Erica", element 1 stores the value "Slavik", and element 2 stores the value "Gary".

Figure 11-1. A sample array structure
figs/act2.1101.gif

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.

11.2.2 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, end, or even middle of an array. An array can have gaps (that is, some elements can be absent). We can have elements at positions 0 and 4, without requiring anything in positions 1, 2, and 3. Arrays with gaps are called sparse arrays.

11.2.3 Array Size

At any point during its life span, an array contains a specific number of elements. The number of elements an array can hold is called the array's length, which we'll discuss later in this chapter.

Arrays in Other Programming Languages

Almost every high-level computer language supports arrays or array-like entities. That said, there are differences in the ways arrays are implemented across different languages. For example, many languages do not allow arrays to contain differing types of data. In many languages, an array can contain numbers or strings, but not both in the same array. Interestingly, in C, there is no primitive string datatype. Instead, C has a single-character datatype named char; strings are considered a complex datatype and are implemented as an array of chars.

In ActionScript, the size of an array changes automatically as items are added or removed. In many languages, the size of an array must be specified when the array is first declared or dimensioned (i.e., when memory is allocated to hold the array's data). Lingo, the scripting language for Macromedia Director, refers to its arrays by the name lists. Like ActionScript, Lingo allows arrays to contain data values of differing types, and it resizes its arrays automatically as needed. Unlike ActionScript and C, in which the first item in an array is numbered 0 (i.e., indexes are zero-relative), the first item in a Lingo list is numbered 1 (i.e., indexes are one-relative).

Languages differ as to what happens when you attempt to access an element outside the bounds (limits) of the array. ActionScript and Lingo add elements if you attempt to set a value for an element beyond the existing bounds of the array. If you attempt to access an element outside the array bounds, ActionScript returns undefined, whereas Lingo issues an error message. C pays no attention to whether you are accessing a valid element number. It lets you retrieve and set elements outside the bounds of the array, which usually causes you to overwrite other data in memory or access meaningless data that is not part of the array (C gives you plenty of rope with which to hang yourself). C even lets you specify a negative index, whereas ActionScript does not.


Table of Contents