473,395 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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<Tis 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
Jun 12 '07 #1
8 5109
Rex <Re**********@OneSimpleHabit.comwrote in
news:co********************************@4ax.com:
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<Tis 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[i] = new List<int>();

-mdb
Jun 12 '07 #2
Type-safe but it is not truly necessary). So if not List<T>, what do
you recommend I use?

A DataTable

Jun 12 '07 #3
On Jun 12, 11:15 am, Rex <RexForum4...@OneSimpleHabit.comwrote:
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<Tis 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<Twhere T is a user defined class that has two
properties.

Jun 12 '07 #4
"Rex" <Re**********@OneSimpleHabit.comschrieb im Newsbeitrag
news:co********************************@4ax.com...
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<Tis 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.
Jun 12 '07 #5
Rex
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<Tbut 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
On Tue, 12 Jun 2007 11:15:23 -0400, Rex
<Re**********@OneSimpleHabit.comwrote:
>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<Tis 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
Jun 12 '07 #6

"Rex" <Re**********@OneSimpleHabit.comwrote in message
news:i5********************************@4ax.com...
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<Tbut 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[i].Add(element);
Rex
On Tue, 12 Jun 2007 11:15:23 -0400, Rex
<Re**********@OneSimpleHabit.comwrote:
>>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<Tis 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

Jun 12 '07 #7
Rex
I think I get it - very simple! I sure appreciate it, Ben and Michael
(and everyone else),
Rex

On Tue, 12 Jun 2007 13:04:13 -0500, "Ben Voigt [C++ MVP]"
<rb*@nospam.nospamwrote:
>
"Rex" <Re**********@OneSimpleHabit.comwrote in message
news:i5********************************@4ax.com.. .
>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<Tbut 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[i].Add(element);
>Rex
On Tue, 12 Jun 2007 11:15:23 -0400, Rex
<Re**********@OneSimpleHabit.comwrote:
>>>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<Tis 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
Jun 12 '07 #8
On Jun 12, 1:53 pm, Rex <RexForum4...@OneSimpleHabit.comwrote:
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<Tbut 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

On Tue, 12 Jun 2007 11:15:23 -0400, Rex

<RexForum4...@OneSimpleHabit.comwrote:
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<Tis 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?
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<Class1myList = new List<Class1>();

To add a new value:

Class1 myClass = new Class1();
myClass.A = somevalue;
myClass.B = someothervalue;
myList.Add(myClass);
Jun 12 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for...
4
by: Mikhail N. Kupchik | last post by:
Hi All. I have a question regarding C++ programming language standard. It is related to standard library, not to the core language. Is it portable to instantiate template class std::list<>...
4
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>....
7
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
4
by: Peted | last post by:
I have the following code public enum pdfFlags { PFD_DRAW_TO_WINDOW, PFD_DRAW_TO_BITMAP, PFD_SUPPORT_GDI, PFD_SUPPORT_OPENGL, PFD_GENERIC_ACCELERATED, PFD_GENERIC_FORMAT,
35
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the...
6
by: =?Utf-8?B?UGhpbA==?= | last post by:
I have seen the samples for Find that explain how to use the predicate, but they are always searching for a pre-defined value. What I don't understand is how to search for a random value stored in...
11
by: paul.gibson | last post by:
A simple code example is easier than trying to describe the issue. I have: public class myClassA {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.