Click here to Skip to main content
15,885,763 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi everyone,
Is it possible to use such a data structure List<T>[][] listArray
Let me explain in more detail. For example when we use string[][] strArray and
strArray[i][j] gives us a string. Is there any way to do this with List<T>.
i.e ListArray[i][j] should give me a list ? (dictionary may solve my problem but I wonder if there is another way. )
Posted
Updated 8-Oct-20 1:24am
Comments
Shahin Khorshidnia 27-Feb-12 16:49pm    
Creating Your Own Collection Class!
Sander Rossel 27-Feb-12 17:37pm    
Actually I have written an article about implementing your own collections (including a Dictionary). If you're interested you can read it. Find it at my articles, it's called 'Having fun with custom collections!' :)
Shahin Khorshidnia 27-Feb-12 18:07pm    
Of course I'll read it.

How about a List<List<T>>? :)
I guess having a list of lists is more or less the same as having a two-dimensional array.
List<T>[][] is also perfectly valid, although you'd get a lot of lists.
C#
List<List<string>> lists = new List<List<string>>();
lists[0].Add("Hello");
string test = lists[0][0]; // Should return "Hello".

Edit:
So about the Dictionary<TKey, TValue>[^] option, you could have a Dictionary inside a Dictionary.
You say you have a day, so let's say day 1, day 2, day 3... In this case a day is an integer value.
For any given day you have a time, perhaps you are using TimeSpan[^] for this.
Also, my guess is that for every day-time combination you want to store a value, let's say a string (but it could be anything). You would then have a Dictionary like this:
C#
Dictionary<int, Dictionary<TimeSpan, string>>
string myString = myDictionary[1][myTime] // Day 1 at that specific time.
Let's say your day is a specific date. Your Dictionary would then look like:
C#
Dictionary<DateTime, Dictionary<TimeSpan, string>>
string myString = myDictionary[DateTime.Today][myTime] // Today at that specific time.
Usage is pretty easy compared to working with a multidimensional array I think (but that's just a matter of what you're accustomed to) :)
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 27-Feb-12 16:48pm    
This is a valid suggestion, my 5, but formally one can use List<T>[][] (array of arrays of instances of List<T>) or anything like that.
--SA
Sander Rossel 27-Feb-12 16:53pm    
Thanks! Sure you can use it, but it's not the easiest method (well, if you're not used to (multi-dimensional) arrays) :)
mstk_ 27-Feb-12 16:53pm    
Thank you for quick reply. But as I said above lists[0][0] should return me a list not a string. Because I want to use days as colums and times(9:00-18:00) as rows. But you say "List<t>[][] is also perfectly valid". I'll try it soonest I hope it makes my job easier. :)
Sander Rossel 27-Feb-12 16:57pm    
You could also try Dictionary<int, Dictionary<TimeSpan, string>>
You could then say: string myString = myDictionary[1][myTime]
wizardzz 27-Feb-12 16:59pm    
Look at SAK's suggestion then.
You can use (data_type array_name [][]=new data_type [rows_size][cols_size])
 
Share this answer
 
Hi

I guess below explanation works for my requirement which is a kind of two dimentional list array.

Requirement:-
In a "Tab control" Every "TabPage" should have one browser which means
TabPage1--> Browser1
TabPage2--> Browser2

Solution (which helps in simulating two dimensional list array):-

Step 1:- Creating a struct
struct myoownobjects
{
Tabpage tbp
geckofx gkbrw
}

Step 2:- Creating list array(single dimension) of strcut object

List<myoownobjects> lst1 = new List<myoownobjects>();


I guess this might also work.
 
Share this answer
 
If you know how many lists you will be needing then how about like array[NameList,AgeList,PointList]? The lists can change dynamicly anyway
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900