What you maybe already know:
Vector class is a powerful class introduced with flash player 10. It is fundamentally a special typed array into which all elements must be the same type and it is faster than a normal Array.
What you maybe don’t know:
It’s possible to initialize a Vector with an Array or another Vector as a source but you must use the Vector function not the class constructor. The class constructor in fact has the following signature:
1 | Vector(length:uint = 0, fixed:Boolean = false) |
Vector function instead has this:
1 | Vector(sourceArray:Object):Vector.<T> |
So, rather than using such approach:
1 2 3 4 5 | var vector:Vector.<String> = new Vector.<String>(); vector[0] = "value1"; vector[1] = "value2"; vector[2] = "value3"; // ...and so on |
We can do the following (Notice the absence of “this” keyword):
1 2 3 4 5 6 7 8 9 | // create a vector with an inline Array as source var vector:Vector.<String> = Vector.<String>(["v1", "v2", "v3"]); // create a vector with an inline Vector as source var vector2:Vector.<String> = Vector.<String>(vector); // Testing (both trace will print the same string) trace(vector[1]); trace(vector2[1]); |


April 29th, 2009 12:05 pm
Thanks
June 13th, 2009 1:30 pm
The best information i have found exactly here. Keep going Thank you
January 2nd, 2010 5:58 pm
BEWARE of instantiating your Vectors in this way as it exhibits random casting behaviours and using an incorrect datatype will NOT throw a compile Error, it will sometimes silently cast the datatype to that specified in the Vector and default to its construction value. Also, compilers will generally give you the Vector specified data type API (as expected) causing invalid method invocation and potential bugs.
The following two examples show this in action:
var vector:Vector. = Vector.( ["hello", new Point4d(), "world"] );
trace( vector[1] );
The above code traces the toString(): String return value contained in my Point4d object, however:
var vector:Vector. = Vector.( [ 5, new Point4d(), 4] );
trace( vector[1] );
traces 0
Yikes, to conclude, if you want compile type data type checking and no silent hidden data casting, avoid this at all costs.
Gary Paluk aka RipX
January 3rd, 2010 2:36 pm
Please note, the type declairations were stripped out of the example code given above. The first example was cast as String and the second example were cast as uint.
Gary Paluk aka RipX
November 16th, 2010 1:15 pm
Thanks very much, saved me time when I rewrote a 2D Array into a Vector.<Vector.>.
Otherwise I’d have had to feed my World-Vector from an Array which sounded awkward to me ;-)
December 26th, 2011 9:03 am
How about this?
var vector:Vector. = new ["v1", "v2", "v3"];
A bit shorter than this one.
var vector:Vector. = Vector.(["v1", "v2", "v3"]);