Chapter 8. Arrays

FEATURED CLASSES

  • Array

In this chapter, you'll look at arrays. Arrays contain indexed data, like a numbered list of items. By using an array, you can store multiple pieces of data in a single variable, which allows you to group values that should go together. An array also provides methods and properties that let you edit it, search through it, sort it, and operate on its contents. Arrays are key tools in any programmer's toolbox.

Arrays are the first complex data type that you study in Part II, "Core ActionScript 3.0 Data Types." Strings, numbers, Booleans, and the like are all primitive data types—the core building blocks of information that usually contain a single piece of data of a specific type. Complex data types, on the other hand, are composites of the various primitive types.

Array Basics

Arrays are a lot like a numbered list of items. Each item, or element, in an array has a location, or index. Unlike most numbered lists, indices in an array start at 0 instead of 1. These indices are used to look up elements in the array, as shown in Figure 8-1.

Using the Array Constructor

Let's create the first array and fill it with information. For this, you're going to use the Array constructor.

var myArray:Array = new Array();

Tip

Remember that a constructor is a function that creates an instance of a class. A constructor is always named after the class it constructs. To use a constructor, use the new keyword followed by the constructor name—for example, new Array().

Figure 8.1. To ...

Get ActionScript 3.0 Bible 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.