0

I'm having problems translating this piece of code: float [,] varname;

to something using List<>

basically i want a way of creating bi-dimensional generic list with that kind of setup

2 Answers 2

3

You have to write:

List<List<float>> list;

Take care that in this case each outer list can have different-sized inner list, is not the same as an array..Also the inner-list can be null.

for the initialization use:

List<List<float>> f = new List<List<float>>();
f.Add(new List<float>());
//add other lists
Sign up to request clarification or add additional context in comments.

Comments

0

I don't know of a 2D list implementation, but you can achieve somewhat similar behaviour with a "jagged" list, i.e. a list of lists:

List<List<float>> varname;

It brings some problems though, for example varname[n] can be null, or varname[n][m] can have m out of range for some arrays, etc. You would have to write some more complex accessors to take care of these states. Even the initialization is a bit more complex.

If there's no conceptual issue with using an array, I'd stick with the array.

2 Comments

maybe i'm complicating things, but i wanted to save some performance, so i decided to do a test using 2D arrays and 2D lists to see if it was worth the effort
Well, the performance depends heavily on the list/array size changes. If there are no size changes, array wins hands down. If the size(s) may change, you can initialize the lists with an initial size of their internal array

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.