How do I initialize a vector with an array of values?
Asked Answered
R

4

6

How do I initialize a vector with an array of values?

I tried this and it complies fine, but does not work!

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

I also need to load an arbitrary array into a vector, like this:

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

Is there a way to do this?


Edit: How do I initialize a 2D vector with a 2D array of values?

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

I tried this but it gives me the error:

TypeError: Error #1034: Type Coercion failed

Randallrandan answered 13/11, 2010 at 17:42 Comment(1)
The variable langs is strongly-typed as a vector of type string, and after I step over the line in FlashDevelop, the values don't show up in the Watch window, and the length is 0.Randallrandan
S
3

I don't think that you can pass in an array of arrays into the Vector:

Vector.<Vector.<Number>>

The type coercion doesn't work for a complex type. If you already have the 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 cause new copies of everything to be created twice, so ideally you are not converting big lists.

Slaughter answered 14/11, 2010 at 8:47 Comment(0)
M
49

The appropriate syntax for initializing a Vector of Strings is this:

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

In order 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 casts it to a Vector, which is slower, has issues with very large Arrays, and doesn't support multidimensional Vectors.

var langs:Vector.<String> = Vector.<String>( [ "en","fr" ] );
Md answered 7/6, 2011 at 14:5 Comment(0)
N
4

You can initialise a Vector.<T> from an array by using the Vector.<T> global function:

var vec : Vector.<String> = Vector.<String>(["en","fr"]);
Nanettenani answered 13/11, 2010 at 18:21 Comment(2)
How do I transfer a 2D-dimensional array into a Vector of Vector of Numbers?Randallrandan
@Jenko - I think @martineno's answer is the closest you'll get.Nanettenani
S
3

I don't think that you can pass in an array of arrays into the Vector:

Vector.<Vector.<Number>>

The type coercion doesn't work for a complex type. If you already have the 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 cause new copies of everything to be created twice, so ideally you are not converting big lists.

Slaughter answered 14/11, 2010 at 8:47 Comment(0)
B
2

The cleanest, fastest and most type-safe way to initialize a Vector from a list of values is :

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

it will not create a temporary Array to initialize the new Vector, so it will generate the fastest bytecode, and will not bother the garbage collector with useless temporary Array instantiations. It's as fast as, but 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 run time errors

For 2D Vectors, there is no direct syntax, so you'll have to explicitly create each Vector:

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

You can have in-depth information on 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 bv a space:

new <Vector.<Number>>

Barracuda answered 25/6, 2014 at 16:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.