- Home /
Javascript Array to Generic List
I'm trying to convert a javascript array into a generic list. I'm having trouble with adding items in the definition. In javascript I have var myarray=New Array("walk","run" etc...);
for generic list I would do
import System.Collections.Generic;
var myarray=new List.("walk","run" etc...); gives an error that the constructor cannot take String,String.
Is there away to add items in the definition without having to use myarray.Add("walk");
Thanks,
Answer by Eric5h5 · Feb 27, 2012 at 05:29 AM
var aList = new List.<String>(["walk", "run", "etc."]);
Although technically that's creating a fixed-size array and using the array to initialize the List. Same difference really.
You might be better off with a 2D array:
var array2D = new String[100, 100];
Otherwise,
var aList = new List.<List.<String> >();
aList.Add(new List.<String>(["a", "b", "c"]));
print (aList[0][0]);
Note the space in the > >
part; that's actually necessary for some reason.
I've seen some people taking built in arrays and then converting them into javascript arrays and back again if they want to push something or removeAt or use the nice javascript functions. Is that going to be slower than using the .NET List?
You can always convert Lists to built-in arrays and back; there's no reason to use Javascript Array. $$anonymous$$ostly you wouldn't need to; Lists are much faster than Javascript Arrays.