清单:预先限制的项目数 [英] List: Limit number of items in advance

查看:86
本文介绍了清单:预先限制的项目数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我想设置一个列表,它最多可以容纳1000个项目.

I want to set a list that it can hold up to 1000 items (for example).

在将所有项目实际添加到列表之前,将对其进行预先分配.

All items will be pre-allocated before actually adding them to the list.

添加项目编号1001后,将从列表中删除项目1.

Upon adding item number 1001, item 1 will be dropped from the list.

因此,用户每时每刻最多可以检查1000个项目.

So every moment the user can inspect up to 1000 items.

有可能吗?

现在我要添加:

MyItem Item = new MyItem();

Item.Name = 1;
Item.ImagePath = "icon1.png";
List.Items.Add(Item)

我不想每次使用 new 来添加项目,而是使用一组预先分配的项目.

I do not want to use new each time I want to add an item but use a pool of pre-allocated items.

此致

ZV

推荐答案

您可以执行removeat来删除位于特定索引处的项目,并且可以检查计数查看列表包含多少项.

You can do removeat in order to remove whichever item is at a particular index and you can check the count to see how many items a List contains.

这意味着您可以执行以下操作:

Meaning you can do something like:

MyList.Add(item);
if (MyList.Count > 1000)
    MyList.RemoveAt(0);


这篇关于清单:预先限制的项目数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆