473,569 Members | 2,991 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5114
Rex <Re**********@O neSimpleHabit.c omwrote in
news:co******** *************** *********@4ax.c om:
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...@O neSimpleHabit.c omwrote:
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**********@O neSimpleHabit.c omschrieb im Newsbeitrag
news:co******** *************** *********@4ax.c om...
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**********@O neSimpleHabit.c omwrote:
>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**********@O neSimpleHabit.c omwrote in message
news:i5******** *************** *********@4ax.c om...
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**********@O neSimpleHabit.c omwrote:
>>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.nos pamwrote:
>
"Rex" <Re**********@O neSimpleHabit.c omwrote 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...@O neSimpleHabit.c omwrote:
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...@O neSimpleHabit.c omwrote:
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<Class1myLi st = new List<Class1>();

To add a new value:

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

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

Similar topics

14
5592
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 storing vertices of an arbitrary n-ary tree where a vertex contain pointers to its parent / children. These parent / child vertices need to stay put...
4
2466
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<> with incomplete type? I've seen some STL implementations which allow this and some others that does not. I did not find any mentioning of this topic...
4
52943
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>. However, I need to cast that list to a List<BaseClass> and it is not working. The code is below. I get the following exception: "Unable to cast...
7
57530
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 without itterating through the entire collection and building up a list? 2. is there a common base class, collection or interface that can contain...
45
18809
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 of their elements? Why can't I change the element itself? class Program { private struct MyStruct
4
10606
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
5852
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 list. That part makes a reasonable amount of sense, as you can't actually take items away from an Array. However, there doesn't seem to be a way to...
6
2676
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 a variable. For example, List<intitems; int val=3; items.Add(1);
11
4998
by: paul.gibson | last post by:
A simple code example is easier than trying to describe the issue. I have: public class myClassA {
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7679
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.