How to use List<T> with 2 dimensions (with the 2nd one variable)?

  • Thread starter Thread starter Rex
  • Start date Start date
R

Rex

Hi All, I need to create a 2-dimensional group of values, the first
dimension having 8 rows, while the 2nd is variable. I was thinking of
using List<T>, but MAYBE List<T> is not the way to go (I prefer
Type-safe but it is not truly necessary). So if not List<T>, what do
you recommend I use?
Thanks!
Rex
 
Hi All, I need to create a 2-dimensional group of values, the first
dimension having 8 rows, while the 2nd is variable. I was thinking of
using List<T>, but MAYBE List<T> is not the way to go (I prefer
Type-safe but it is not truly necessary). So if not List<T>, what do
you recommend I use?
Thanks!
Rex

How about:

List<int>[] myList = new List<int>[8];

Don't forget to initialize each value of the array:

for(int i=0; i<myList.Length; i++) myList = new List<int>();

-mdb
 
Hi All, I need to create a 2-dimensional group of values, the first
dimension having 8 rows, while the 2nd is variable. I was thinking of
using List<T>, but MAYBE List<T> is not the way to go (I prefer
Type-safe but it is not truly necessary). So if not List<T>, what do
you recommend I use?
Thanks!
Rex

I would use List<T> where T is a user defined class that has two
properties.
 
Rex said:
Hi All, I need to create a 2-dimensional group of values, the first
dimension having 8 rows, while the 2nd is variable. I was thinking of
using List<T>, but MAYBE List<T> is not the way to go (I prefer
Type-safe but it is not truly necessary). So if not List<T>, what do
you recommend I use?
Thanks!
Rex

You can use:
T[][]
List<T>[]
List<T[]>
List<List>>

they or all type safe

List<> can change in length (without creating a wole new list), while array
can't. Still different arraytype element of a list (List<> or array) can
differ in length.

So I would recommend

T[][]

if the length of each roew doesn't change.

List<T>[]

if the length of each row can change over time

HTH

Christof.
 
Thank you, Guys, for the help. I'm now going to show you how I'm still
somewhat of a Newbie in C#: I have successfully used List<T> but I'm
not yet clear at the detail level about your offered solutions. I
think that upon reflection I will MOSTLY get what you-all suggested.
But I think the KEY statement that would help me is so see an ADD
using any of these solutions. So can anyone give me, say, the
declaration and then a sample .Add statement ??? I sure would
appreciate it,
Rex
 
Rex said:
Thank you, Guys, for the help. I'm now going to show you how I'm still
somewhat of a Newbie in C#: I have successfully used List<T> but I'm
not yet clear at the detail level about your offered solutions. I
think that upon reflection I will MOSTLY get what you-all suggested.
But I think the KEY statement that would help me is so see an ADD
using any of these solutions. So can anyone give me, say, the
declaration and then a sample .Add statement ??? I sure would
appreciate it,

Using Michael Bray's suggestion of an array of List<T>, which is the best
for your situation:

Following his code, to add an element to row i, do:

myList.Add(element);
 
I think I get it - very simple! I sure appreciate it, Ben and Michael
(and everyone else),
Rex

Rex said:
Thank you, Guys, for the help. I'm now going to show you how I'm still
somewhat of a Newbie in C#: I have successfully used List<T> but I'm
not yet clear at the detail level about your offered solutions. I
think that upon reflection I will MOSTLY get what you-all suggested.
But I think the KEY statement that would help me is so see an ADD
using any of these solutions. So can anyone give me, say, the
declaration and then a sample .Add statement ??? I sure would
appreciate it,

Using Michael Bray's suggestion of an array of List<T>, which is the best
for your situation:

Following his code, to add an element to row i, do:

myList.Add(element);
 
Thank you, Guys, for the help. I'm now going to show you how I'm still
somewhat of a Newbie in C#: I have successfully used List<T> but I'm
not yet clear at the detail level about your offered solutions. I
think that upon reflection I will MOSTLY get what you-all suggested.
But I think the KEY statement that would help me is so see an ADD
using any of these solutions. So can anyone give me, say, the
declaration and then a sample .Add statement ??? I sure would
appreciate it,
Rex

Say you have a class with two properties. Class name is Class1, and
property names are A and B. You also have a varible defined as:

List<Class1> myList = new List<Class1>();

To add a new value:

Class1 myClass = new Class1();
myClass.A = somevalue;
myClass.B = someothervalue;
myList.Add(myClass);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top