Agreed. There’s this, but it would be nice if something like that was in the docs.
–Eric
Agreed. There’s this, but it would be nice if something like that was in the docs.
–Eric
if you can share an example how to convert array to list it would be great
In JS, here’s how you do 1D and 2D lists:
import System.Collections.Generic; // necessary when using lists
// declare lists
var list1D = new List.<int>(); // a list of ints
var list2D = new List.< List.<float> >(); // a 2D list of floats
// add elements
list1D.Add(someData);
list2D.Add(list1D);
// refer to elements
xx = list1D[x];
yy = list2D[x][y];
A couple more “how-to” list questions:
How can I initialize a list with a set of constants, for example:
list1D = [0.0, 1.0, 2.0, 3.0, 4.0];
AddRange doesn’t work, or at least I can’t figure out the syntax. The only solution I’ve found is pretty awkward with more than a few values:
list1D.Add(0.0); list1D.Add(1.0); list1D.Add(2.0); list1D.Add(3.0); list1D.Add(4.0);
Also, is it possible to initialize a list with a fixed number of elements? Something comparable this:
newArr = new float[1300];
For example, I don’t always need to populate the entire list all at once, but do need to be able to stick a value into a particular location in the list. So I might initialize the list, then over time populate it one element at a time. Is this possible?
var list1D = new List.<float>([0.0, 1.0, 2.0, 3.0, 4.0]);
That sounds like you want an array, not a list. You can do “var list1D = new List.(1300);” if you know the list will contain a minimum of 1300 elements, but all that does is pre-allocate the memory (in order to prevent multiple allocations when adding one element at a time).
–Eric
In a previous program that did a lot of number crunching, converting from arrays to lists sped things up considerably. This is another program that does heavy-duty number crunching, so I was hoping to speed it up too. I’ve figured out ways around these list/arrays differences, but thought there might be more proper ways of setting up lists.
Same problem with duplicating a list (a copy, not a reference). There is no Copy method, so it’s a little less direct than with an array but can be done.
Are you talking about the JS Array class? Built-in (non-resizable) arrays are faster than Lists.
var list1 = new List.<String>([0.0, 1.0, 2.0, 3.0, 4.0]);
var list2 = new List.<String>(list1); // A copy of list1
–Eric