Bits & Bytes

Declaring and Using Vectors in Actionscript 3.0

Actionscript allows us to use a parameterized Vector class for an array-type data structure, in addition to the Array class. Unlike an Array, a Vector can only hold one type of data item. This restriction allows a Vector to be accessed more efficiently than an Array. So, a Vector should be used whenever possible.

A Vector in Actionscript is similar to an STL vector in C++. Both are parameterized by a data type and can grow or shrink as items are added or removed. Both the Actionscript and C++ types have many different operations for manipulating the vector types. Both vectors also use the less-than and greater-than symbols to bracket the parameter type. However, the Actionscript Vector requires an additional period before the parameter, as shown in the examples below.

One-Dimensional Vectors

Function Notation

  1. var aMyVector:Vector.<int> = Vector.<int>([1, 2, 3]);
  2. var aMyArray:Array = Array(new Sprite());
    var aMyVector:Vector.<DisplayObject> = Vector.<DisplayObject>(aMyArray);

Constructor Notation

  1. var aMyVector:Vector.<int> = new Vector.<int>();
    aMyVector.push(1, 2);
  2. var aMyVector:Vector..<int> = new Vector.<int>(2);
  3. var aMyVector:Vector.<int> = new Vector.<int>(2, true);
    aMyVector[0] = 1;
    aMyVector[1] = 2;

Multi-dimensional Vectors

Above, we have several examples of how to create a Vector. The first example uses the Vector() function to create a Vector of 3 ints with the values 1, 2, and 3. The second example uses the Vector() function to create a Vector of the base type DisplayObject from an Array of the derived type Sprite.

Using constructors, we have three examples below those; the constructor takes two parameters: an uint and a Boolean to set the size and the whether the size is fixed, respectively. Without any parameters, the constructor uses the default parameters 0 and false to create a Vector of size zero that can be resized, as we show in the first constructor notation example. In the second constructor example, we create a Vector of ints with size 2. Since we did not specify a second argument, the Vector does not have a fixed size. In the last example, we create a Vector of ints with size 2 that has a fixed size, since the second parameter is true. We use the bracket operator to fill the first and second entries with 1 and 2, respectively.

  1. var aaMyVector:Vector.<Vector.<int>> = new Vector.<Vector.<int>>(3, true);
    aaMyVector[0] = new Vector.<int>(2, true);
    aaMyVector[1] = new Vector.<int>(2, true);
    aaMyVector[2] = new Vector.<int>(2, true);

    aaMyVector[0][0] = 1;
    aaMyVector[1][0] = 2;
    aaMyVector[2][0] = 3;
    aaMyVector[0][1] = 4;
    aaMyVector[1][1] = 5;
    aaMyVector[2][1] = 6;

Just as we did for Arrays, we can create multi-dimensional Vectors recursively. We can combine any of the methods for creating one-dimensional Vectors to do this. However, we limit ourselves to one example to avoid confusion. In this example, we create a Vector of 3 int Vectors. Next we create a new Vector of ints for each entry of this Vector. Finally, we use the double bracket operator to assign each entry a value.

Tags: , , , , , , ,

Michael Hall

By: Michael Hall

One Response to “Declaring and Using Vectors in Actionscript 3.0”

  1. Ravi Bhadauria says:

    nICE eXPLANATION
    tHANKS

Leave a Reply

*

 

© 2007–2024 XoaX.net LLC. All rights reserved.