tags:

views:

125

answers:

6

I need to assemble an list of lists, but the whole idea doesn't sound too pretty. It just sounds so cumbersome. is there some other pattern for holding a list of lists.

my first though is to use an arrayList of Arraylist.

c#, .net-2

more: then number of items to be stored is small but always changing.

NOTE: I was corrected on the use of ArrayLists on this Question:

What's wrong with using an ArrayList in .net-2.0

+4  A: 

There's nothing wrong with List<List<T>> - LINQ's SelectMany can be your friend in that situation.

Stephen Cleary
While the comment is valid (and gets a +1), LINQ is only available in 3.5 and above, but the OP listed 2.0.
Adam Robinson
He says it's .Net 2.0.
DOK
You can use LinqBridge (or similar) for Linq to Objects in .Net 2.0
DaveShaw
+1  A: 

Not a problem at all. I've already used it and it was a fit for what I needed at the time. The pattern is a list of lists. :)

Leniel Macaferi
+1  A: 

A list of lists is not, of itself, a bad smell. If your lists are all going to be of the same size, you may want to use a 2D array e.g. int[2,2], but if the lists are of different lengths, then a list of lists is the right way to go, short of formally coding a class for a ragged 2D array.

Dr Herbie
You could also use jagged arrays in C# directly - no need to "formally code a class for a ragged 2D array" - jagged arrays are supported already in C#.
Reed Copsey
+2  A: 

How about using objects created in custom classes?

For example, Customers can have multiple Addresses. Create a Customer object which has an Address property. Then, you can have a collection (array, ArrayList, etc) of Customers, and each Customer can have a collection of Addresses.

This fits many kinds of information, such as Products in Product Categories, Employees in Departments.

It's easier in coding to handle the hierarchical relationship this way.

DOK
+1  A: 

you can but it'd better to wrap it to a class with well defined public methods.

Arseny
+1  A: 

You can certainly do that either using generic lists or the non-generic variant, ArrayList.

List<List<string>> listOfLists = new List<List<string>>();
listOfLists.Add(new List<string>());
listOfLists.Add(new List<string>());

ArrayList stringListOfStringLists = new ArrayList();
stringListOfStringLists.Add(new ArrayList());
stringListOfStringLists.Add(new ArrayList());
Jeff M
-1 for mentioning that ArrayList is an option. Rule of thumb, never use ArrayList.
tster
Why not? What's the problem with ArrayList?
John Kugelman
@John Kugelman - Type safety, boxing and unboxing costs i.e. performance
Russ Cam
@tster, fair enough. I suggested ArrayList partly because he mentions it in the question but mostly for the possibility of seeing it in legacy code being a .net 2.0 project. I should have mentioned that it should not be used.
Jeff M