follow me on twitter

Note to self: remember to use the test database when testing this site ;D


final test - hope

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.".




Saeed Ashour via the old blog says ... Hi ...

may i ask you how did you know about Vector data type?

Bryan Bartow via the old blog says ... ECMA Script bro.

Saeed Ashour via the old blog says ... Damn bro :) ! thanks for confirm bro

Aaron via the old blog says ... What's with the underscore in the name of _displayChildren? Does that do something different than naming it just displayChildren?

admin via the old blog says ... Aaron, the underscore is simply a way for me to recognize private member variables.

Matt Taylor via the old blog says ... Hi,

Are you aware of any speed tests that have been done with the Vector type? ( thinking software rasterizer...)

Luca Deltodesco via the old blog says ... 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?

juan via the old blog says ... I was an aethiest, but now that there are typed collections in action script I DO believe that god exists. Its about time.

Vector class available in Flash 10 « know SWF via the old blog says ... [...] see here [...]

henry via the old blog says ... Thanks man just what i needed :)

Vern via the old blog says ... 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.

hydrotik | flash development | actionscript | creative » Blog Archive » 11 Must Have Flash Resources and 11 Must Have Utilities For 2009 via the old blog says ... [...] 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 [...]

Three Months of ActionScript | Software and Opinions via the old blog says ... [...] 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 [...]

Usando Vector em AS3 para Flash Player 10 | Bruno Soares via the old blog says ... [...] 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 [...]

Jordan via the old blog says ... 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.

Hipolito M. Wiseman via the old blog says ... Well, the article is actually the greatest on this precious topic. I fit in with your conclusions and will thirstily look forward to your next updates on back child support. Just saying thanks will not just be enough, for the tremendous lucidity in your writing. I will directly grab your rss feed to stay abreast of any updates. Good work and much success in your business enterprize!

ANGELITABennett via this site says ... Every body understands that men's life is very expensive, however some people need money for different issues and not every person gets enough cash. Thence to receive good <a href="http://lowest-rate-loans.com">loans</a> or just student loan will be a proper solution.







Coming Soon