
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
The Anatomy of an Array
|
247
When we work with an array (the chest of drawers), we usually are interested in the
values within the array (the contents of the drawers). But we can also manipulate the
containing structure itself. For example, we can change an array’s size (add or sub-
tract drawers) or reorder its values (swap the contents of its drawers).
Though we speak of an array as containing many values, it’s important to recognize
that the array itself is a single datum, in the same way that a string containing multi-
ple characters is still a single string, and a number containing several digits is still a
single number. As a single datum, an array can be assigned to a variable or used as
part of a complex expression:
product = ["ladies downhill skis", 475]; // Store an array in a variable
display(product); // Pass that array to a function
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.
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 hav-
ing a different name, each item has an element number (the first element is number
0, not number ...