13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with arrays<br />

As a result of its restrictions, a Vector has three primary b<strong>en</strong>efits over an Array instance whose elem<strong>en</strong>ts are all<br />

instances of a single class:<br />

Performance: array elem<strong>en</strong>t access and iteration are much faster wh<strong>en</strong> using a Vector instance than wh<strong>en</strong> using an<br />

Array instance.<br />

Type safety: in strict mode the compiler can id<strong>en</strong>tify data type errors. Examples of such errors include assigning a<br />

value of the incorrect data type to a Vector or expecting the wrong data type wh<strong>en</strong> reading a value from a Vector.<br />

At run time, data types are also checked wh<strong>en</strong> adding data to or reading data from a Vector object. Note, however,<br />

that wh<strong>en</strong> you use the push() method or unshift() method to add values to a Vector, the argum<strong>en</strong>ts’ data types<br />

are not checked at compile time. Wh<strong>en</strong> using those methods the values are still checked at run time.<br />

Reliability: runtime range checking (or fixed-l<strong>en</strong>gth checking) increases reliability significantly over Arrays.<br />

Aside from the additional restrictions and b<strong>en</strong>efits, the Vector class is very much like the Array class. The properties<br />

and methods of a Vector object are similar—for the most part id<strong>en</strong>tical—to the properties and methods of an Array.<br />

In most situations where you would use an Array in which all the elem<strong>en</strong>ts have the same data type, a Vector instance<br />

is preferable.<br />

Creating arrays<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

You can use several techniques to create an Array instance or a Vector instance. However, the techniques to create each<br />

type of array are somewhat differ<strong>en</strong>t.<br />

Creating an Array instance<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

You create an Array object by calling the Array() constructor or by using Array literal syntax.<br />

The Array() constructor function can be used in three ways. First, if you call the constructor with no argum<strong>en</strong>ts, you<br />

get an empty array. You can use the l<strong>en</strong>gth property of the Array class to verify that the array has no elem<strong>en</strong>ts. For<br />

example, the following code calls the Array() constructor with no argum<strong>en</strong>ts:<br />

var names:Array = new Array();<br />

trace(names.l<strong>en</strong>gth); // output: 0<br />

Second, if you use a number as the only parameter to the Array() constructor, an array of that l<strong>en</strong>gth is created, with<br />

each elem<strong>en</strong>t’s value set to undefined. The argum<strong>en</strong>t must be an unsigned integer betwe<strong>en</strong> the values 0 and<br />

4,294,967,295. For example, the following code calls the Array() constructor with a single numeric argum<strong>en</strong>t:<br />

var names:Array = new Array(3);<br />

trace(names.l<strong>en</strong>gth); // output: 3<br />

trace(names[0]); // output: undefined<br />

trace(names[1]); // output: undefined<br />

trace(names[2]); // output: undefined<br />

Third, if you call the constructor and pass a list of elem<strong>en</strong>ts as parameters, an array is created, with elem<strong>en</strong>ts<br />

corresponding to each of the parameters. The following code passes three argum<strong>en</strong>ts to the Array() constructor:<br />

var names:Array = new Array("John", "Jane", "David");<br />

trace(names.l<strong>en</strong>gth); // output: 3<br />

trace(names[0]); // output: John<br />

trace(names[1]); // output: Jane<br />

trace(names[2]); // output: David<br />

Last updated 6/6/2012<br />

28

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!