Indexed Arrays - Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual

Programming actionscript 3.0
Table of Contents

Advertisement

Third, if you call the constructor and pass a list of elements as parameters, an array is created
with elements corresponding to each of the parameters. The following code passes three
arguments to the Array constructor:
var names:Array = new Array("John", "Jane", "David");
trace(names.length); // output: 3
trace(names[0]); // output: John
trace(names[1]); // output: Jane
trace(names[2]); // output: David
You can also create arrays with array literals or object literals. An array literal can be assigned
directly to an array variable, as shown in the following example:
var names:Array = ["John", "Jane", "David"];
Inserting array elements
Three of the Array class methods—
elements into an array. The
array. In other words, the last element inserted into the array using the
have the highest index number. The
beginning of an array, which is always at index number 0. The
any number of items at a specified index in the array.
The following example demonstrates all three methods. An array named
store the names of the planets in order of proximity to the Sun. First, the
called to add the initial item,
that belongs at the front of the array,
insert the items
Venus
, the integer 1, directs the insertion to begin at index 1. The second argument sent
splice()
to
, the integer 0, indicates that no items should be deleted. Finally, the third and
splice()
fourth arguments sent to
var planets:Array = new Array();
planets.push("Mars");
planets.unshift("Mercury"); // array contents: Mercury,Mars
planets.splice(1, 0, "Venus", "Earth");
trace(planets);
The
and
push()
unshift()
length of the modified array. The
insert elements, which may seem strange, but makes more sense in light of the
method's versatility. You can use the
array, but also to remove elements from an array. When used to remove elements, the
method returns an array containing the elements removed.
splice()
,
push()
method appends one or more elements to the end of an
push()
unshift()
. Second, the
Mars
Mercury
and
after
Earth
Mercury
,
and
splice()
Venus
// array contents: Mars
// array contents: Mercury,Venus,Earth,Mars
methods both return an unsigned integer that represents the
splice()
splice()
, and
unshift()
splice()
method inserts one or more elements at the
splice()
method is called to insert the item
unshift()
. Finally, the
splice()
, but before
. The first argument sent to
Mars
, are the items to be inserted.
Earth
method returns an empty array when used to
method not only to insert elements into an
—allow you to insert
method will
push()
method will insert
is created to
planets
method is
push()
method is called to
splice()

Indexed arrays

229

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents