我有一个应用程序会不断收集用户的输入并将输入存储在List
class ItemsValue
我将如何制作它,以便一旦收集达到1000计数,它将“停止”并创建一个新的集合,等等。
例如:
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.
答案 0 :(得分:4)
我不知道为什么,但你想要一个&#34;列表列表&#34;: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>());
答案 1 :(得分:3)
我认为你需要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);
}
我不知道代码的其余部分是什么样的,但我会将计数器设为静态。
答案 2 :(得分:3)
使用集合列表。如果你有固定的大小,你可以使用数组而不是列表。
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);
}
答案 3 :(得分:2)
试试这个:
List<List<ItemsValue>> collections = new List<List<ItemsValue>>({new List<ItemsValue>()});
if(collections[collections.Count-1].Count >= 1000)
{
collections.Add(new List<ItemsValue>());
}
将项目添加到集合时,请使用上面的if语句。要将项添加到集合,请使用以下命令:
collections[collections.Count-1].Add(yourItem);