11.2. 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.
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, 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".
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.
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 may be absent). We may have elements at positions
and 4, but nothing in positions 1, 2, and 3. Arrays with gaps are
called sparse arrays.
11.2.3. Array Size
Every array contains a specific number of elements at any given point
during its life span. The number of potential elements an array can
hold is called the array's length, which
we'll discuss later.
Arrays in Other Programming Languages
Almost
every high-level computer language
supports some sort of 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 char s!
In ActionScript, the size of an array will change 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., 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 will resize its arrays
automatically as needed. Unlike ActionScript and C, in which the
first item in an array is numbered
(i.e., is zero-relative), the first item in a
Lingo list is numbered 1 (i.e., is
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 will 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 it causes an error in Lingo. 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).
|
| | | 11. Arrays | | 11.3. Creating Arrays |
Copyright © 2002 O'Reilly & Associates. All rights reserved.
|
|