How to initialize a vector with an array of values?

How to initialize a vector with an array of values?

I tried this and it respects perfectly, but does not work!

langs = new Vector.<String>(["en","fr"]); 

I also need to load an arbitrary array into a vector, for example:

  langlist = ["en","fr"]; langs = new Vector.<String>(langlist); 

Is there any way to do this?


Edit: How to initialize a 2D vector using a 2D array of values?

  numbers = [[10,20,30], [10,20,30]]; nums = Vector.<Vector.<Number>>(numbers); 

I tried this, but it gives me an error:

TypeError: Error # 1034: Type forced input error

+6
arrays flash vector actionscript-3 strong-typing
source share
4 answers

I don't think you can pass an array of arrays to a vector:

 Vector.<Vector.<Number>> 

Typical coercion does not work for a complex type. If you already have a 2D array, consider the following conversion code:

 var numbers:Array = [[1, 2, 3], [4, 5, 6]]; var numbersTemp:Array = numbers.map( function (element:*, index:int, arr:Array):Vector.<Number> { return Vector.<Number>(element); }); var nums:Vector.<Vector.<Number>> = Vector.<Vector.<Number>>(numbersTemp); 

Of course, this will lead to the creation of new copies of everything that will be created twice, so ideally you will not convert large lists.

+3
source share

The appropriate syntax for initializing Vector of Strings is as follows:

 var langs:Vector.<String> = new <String>[ "en","fr" ]; 

To create multidimensional vectors, use the following syntax:

 var v:Vector.<Vector.<int>> = new <Vector.<int>>[ new <int>[ 1, 2, 3 ], new <int>[ 4, 5, 6 ] ]; 

Note that the following syntax works, but is less desirable because it first generates an array and then passes it to a vector that is slower, has problems with very large arrays, and does not support multidimensional vectors.

 var langs:Vector.<String> = Vector.<String>( [ "en","fr" ] ); 
+49
source share

You can initialize Vector.<T> from an array using the global function Vector.<T> :

 var vec : Vector.<String> = Vector.<String>(["en","fr"]); 
+4
source share

The cleanest, fastest, and most secure Vector initialization type from a list of values:

 langs = new <String> ["en","fr"]; 

it will not create a temporary array to initialize a new vector, so it will generate the fastest bytecode and will not disturb the garbage collector with useless temporary Array instances. It is as fast as it is more practical than:

 langs = new Vector.<String>(2); langs[0] = "en"; langs[1] = "fr"; 

Most importantly, it will perform type checking at compile time and reduce the chance of getting runtime errors.

There is no direct syntax for 2D vectors, so you will need to explicitly create each vector:

 nums = new <Vector.<Number> > [new <Number>[10,20,30], new <Number>[10,20,30]]; 

You can get detailed information about the performance of non-empty vector initialization and why other solutions are slower here:

http://jacksondunstan.com/articles/702

PS: older mxmlc compilers will not understand the closing brackets if they are not separated by a space:

new <Vector.<Number>>

+2
source share

All Articles