kebrus
kebrus

Reputation: 19

What's the equivalent of this code using array to one using generic lists?

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

Upvotes: 0

Views: 111

Answers (2)

Tobia Zambon
Tobia Zambon

Reputation: 7629

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

Upvotes: 3

Honza Brestan
Honza Brestan

Reputation: 10957

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.

Upvotes: 0

Related Questions