does an list of lists smell bad, what are my other options
I need to asse开发者_如何转开发mble 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
There's nothing wrong with List<List<T>>
- LINQ's SelectMany
can be your friend in that situation.
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.
you can but it'd better to wrap it to a class with well defined public methods.
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. :)
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.
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());
精彩评论