0

I have an application that will continuously taking user's input and store the input in a List of class ItemsValue

How am I going to make it so that once the collection reaches 1000 counts, it will "stop" and a new collection will be created and so on.

For example:

List<ItemsValue> collection1 = new List<ItemsValue>();
//User input will be stored in `collection1`
if (collection1.count >= 1000)
    //Create a new List<ItemsVales> collection2, 
    //and the user input will be stored in collection2 now.
    //And then if collection2.count reaches 1000, it will create collection3. 
    //collection3.count reaches 1000, create collection4 and so on.
C.J.
  • 3,409
  • 8
  • 34
  • 51
  • 2
    You can make list of collections. But what's the original purpose? – Ulugbek Umirov Apr 24 '14 at 15:54
  • 1
    Why? Why not just add into same collection? – Euphoric Apr 24 '14 at 15:54
  • 7
    [XY problem?](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Liam Apr 24 '14 at 15:55
  • 1
    Why not just use Take and Skip? – Liam Apr 24 '14 at 15:56
  • @Liam definitely. This completely sounds like I have a solution to a problem, help me with my solution, not the problem. – Erik Philips Apr 24 '14 at 15:56
  • possible duplicate of [How to loop through IEnumerable in batches](http://stackoverflow.com/questions/15414347/how-to-loop-through-ienumerable-in-batches) – Liam Apr 24 '14 at 15:56
  • 1
    No, we need to temporary store the data in the list then transfer the data into the next application. – C.J. Apr 24 '14 at 15:57
  • 1
    If someone were to look at the question or at any of the answer without reading everything on this page first and use their intuition they would simply state: _Hey ! There's no way that the if statement will ever be true_ or .. _The index access operation [collections.Count - 1] will also throw an exception_. So please: don't be hasty, split the various pieces of code into their rightful places, explain the "event oriented" nature of the operations that are executed against the collection/collections. @Liam: this most surely is an XY problem (+1) – Eduard Dumitru Apr 24 '14 at 16:04
  • @Liam It is not my intention to make this an XY problem. I have a job with a specific requirement, it is not up to me to question my clients about their decisions, even if their request sometimes make no sense, but I'm stuck with the Y. – C.J. Apr 24 '14 at 16:19

4 Answers4

4

I don't know why, but you want a "list of lists": List<List<ItemsValue>>.

List<List<ItemsValue>> collections = new List<List<ItemsValue>>();
collections.Add(new List<ItemsValue>());

collections.Last().Add(/*user input*/);

if (collections.Last().Count >= 1000) collections.Add(new List<ItemsValue>());
Bobson
  • 13,498
  • 5
  • 55
  • 80
3

I think you need List<List<ItemsValue>>

List<List<ItemsValue>> mainCollection = new List<List<ItemsValue>>();
int counter = 0;
if (counter == 0) mainCollection.Add(new List<ItemsValue>());

if(mainCollection[counter].Count < 1000) mainCollection[counter].Add(item);

else 
{
    mainCollection.Add(new List<ItemsValue>());
    counter++;
    mainCollection[counter].Add(item);
}

I don't know how is the rest of your code look like,but I would make that counter static.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
3

Use a list of collections. If you have fixed size you can use a array instead of a list.

List<List<ItemsValue>> collections = new List<List<ItemsValue>>({new List<ItemsValue>()});
if(collections[collections.Count- 1].Count >= 1000)
{
   var newCollection = new List<ItemsValue>();
   // do what you want with newCollection
   collections.Add(newCollection);
}
Nick Udell
  • 2,420
  • 5
  • 44
  • 83
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
  • You can condense your if statement's body to collections.Add(new List()) without losing any real readability. – Nick Udell Apr 24 '14 at 15:59
  • It's not for readability, it's more for doing things with the new List. – Amir Popovich Apr 24 '14 at 16:00
  • Ah I see. Although you could easily get that new list by simply going collections.Last() later. – Nick Udell Apr 24 '14 at 16:01
  • 1
    Yup..nothing wrong with both approaches.. Cheers :) – Amir Popovich Apr 24 '14 at 16:02
  • 1
    Kudos for the compiler error in `collections[collections.Count - 1] >= 1000`, for the irrefutable `IndexOutOfBoundsException` should the code actually compile and for the upvoters' enthusiasm. This is nothing personal: The same goes for the question and the other N-1 answers. – Eduard Dumitru Apr 24 '14 at 16:10
2

Try this:

List<List<ItemsValue>> collections = new List<List<ItemsValue>>({new List<ItemsValue>()});

if(collections[collections.Count-1].Count >= 1000)
{
    collections.Add(new List<ItemsValue>());
}

Use the above if statement when you're adding an item to collections. To add an item to collections, use the following:

collections[collections.Count-1].Add(yourItem);
Nick Udell
  • 2,420
  • 5
  • 44
  • 83