Just use some simple logic. From your code, it looks like you should understand that a generic list is a collection of anything. So, what can act as a collection of variable number of lists? Not surprisingly, a list of lists:
using ColorList = System.Collections.Generic.List<Color>;
using ListOfColorLists = System.Collections.Generic.List<System.Collections.Generic.List<Color>>;
public ListOfColorLists CreateClusters(int numberOfClusters) {
ListOfColorLists list = new ListOfColorLists();
for (int index = 0; index < numberOfClusters; ++index)
list.Add(new ColorList());
return list;
}
Please pay attention for some fixed I've made. First of all, you are trying to execute your loop
NumberOfClusters − 1
times; you would miss one element, so the fix is to start with zero index. I also improved your naming to meet Microsoft naming conventions. Loop variable is renamed because one-character names are bad (how would you do search, for example?);
++index
provides better performance then
index++
without change in results. (It looks strange, but try to test it.) Return value is added to your method, otherwise creation of objects would be pointless and eventually would cause destruction of them by the Garbage Collector (
http://en.wikipedia.org/wiki/Garbage_collector_%28computing%29[
^]).
—SA