new Vector - ActionScript 3’s Typed Array
Flash Player 10 introduces a new core data type to store lists of data called “Vector”. It’s almost identical to the vanilla ActionScript array we use today but with the added benefit of being strictly/strongly typed. Strict typing is very beneficial, it helps create error free code while coding and compiling, allows for understandable APIs and best of all, increases code execution performance.
Vector is a top-level class just as Array is so you will not need to import it.
Below is an example of creating a Vector for the int type.
var vec:Vector.<int> = new Vector.<int>();
Notice that we define the type of Vector with the use of the less-than/lower-than brackets.
Here is an extended example using a Vector for holding Sprites.
class Foo {
private var _displayChildren:Vector.<Sprite>;
function Foo() {
_displayChildren = new Vector.<Sprite>;
}
public function addChild(child:Sprite):void {
_displayChildren.push(child);
}
public function removeChild(child:Sprite):void {
var idx:int = _displayChildren.indexOf(child);
_displayChildren.splice(idx,1);
}
public function get numChildren():uint {
return _displayChildren.length;
}
}
If you attempt to add data of the wrong type to a vector, you shall receive a “Type Coercion failed: cannot convert” error. There is however some exceptions, for example if you have a int typed Vector and you try to add a String to it using a push or unshift method, it shall perform an int(string) conversion.
intVec.push("00123")
intVec.push("hello")
trace(intVec) // 123, 0
If you try this however .. intVec[0] = “00123″ .. it shall result in a “Implicit coercion of a value of type String to an unrelated type int” error.
The only method Vector didn’t pick up from its Array cousin is sortOn. Apart from that, every method from Array is available to use to manipulate Vector.
Vector does introduce something new though, the ability to force a fixed collection length. Below is an example on how to enable fixed length.
// int Vector with fixed length of 5 var vec:Vector.<int> = new Vector.<int>(5, true); trace(vec.fixed) // true trace(vec) // 0, 0, 0, 0, 0
While in fixed mode, the methods push/pop/shift/unshift/splice/..etc shall not work and shall result in a “Cannot change the length of a fixed Vector.” error.
If you go out of the fixed range using the square bracket notation .. vec[15] = 1 .. you shall find this error “The index 15 is out of range 5.”.




Hi …
may i ask you how did you know about Vector data type?
ECMA Script bro.
Damn bro
! thanks for confirm bro
What’s with the underscore in the name of _displayChildren? Does that do something different than naming it just displayChildren?
Aaron, the underscore is simply a way for me to recognize private member variables.
Hi,
Are you aware of any speed tests that have been done with the Vector type? ( thinking software rasterizer…)
I’ve done a test with this, and using the Vector with primitive data types like ‘int’ ive found it to run with very roughly a third the execution time of an Array (both intially sized so resizing needed)
However, worringly : using a non primitive type like Point, i found Array to slightly outperform the Vector….?
Is this going to perhaps be something like ‘uint’ in that at first uint tended to be ridiculously slow until an update got it down to its rightful execution speed in practice?
I was an aethiest, but now that there are typed collections in action script I DO believe that god exists. Its about time.
[…] see here […]
Thanks man just what i needed
man…just got cs4….and changed a whole app’s arrays to vectors….then read the comment about the poorer performance of vectors for non-primitive types…which describe all of the arrays I just converted to vectors!…ahhhh. I guess I should have tested first but it’s so counter intuitive for a type safe data structure to perform worse.
[…] on accessing its contents instead of what it contains. You can find a good example of this at Alex Bustin’s Blog. Also in recent news from Adobe, Alchemy (which allows you to compile C and C++ code into AS using […]
[…] see that they’re extending the language to support limited generics support so hopefully that won’t be the end of it. I’m looking forward to experimenting more on […]
[…] Vector Teste de performance Post sobre Vector por: Mike Chambers Post sobre Vector por: DaveOnCode Post sobre Vector por: The Back Button Posts relacionados:JPEG Encoder (AS3) + FluorineFx .NET Flash Remoting GatewayFluorine, uma […]
Have you been able to get code hinting working for vector objects? I’m on OS X using FB 3. I installed the SDK fine and I can get my code to run, I just don’t get code hinting for the vector class. I’m on SDK version 4.0.0.7219.