简体   繁体   English

在开始时高效收集插入和移除

[英]Efficient collection for inserts and removals at the beginning

What collection would you recommend for a code that frequently inserts and removes objects at the beginning of the collection only.对于仅在集合开头频繁插入和删除对象的代码,您会推荐什么集合。

Here is some code to illustrate my requirements这是一些代码来说明我的要求

while (collection.Count != 0)
{
   object obj = collection[0];
   collection.RemoveAt(0);

   ...

   if (somethingWith(obj))
       collection.Insert(0, anotherObj);

   ...  
}

There is no inserts or removals at positions other than 0. Collection is not sorted.在 0 以外的位置没有插入或删除。集合未排序。

What would you recommend?你会推荐什么?

EDIT:编辑:

I don't really need to do anything fancy with the collection.我真的不需要对这个系列做任何花哨的事情。 The collection is used to queue objects that should be processed (and collection gets populated during processing).该集合用于对应处理的对象进行排队(并且在处理期间填充集合)。

It seems you only want to implement a LIFO container, so you can use a Stack<T> :似乎您只想实现一个LIFO容器,因此您可以使用Stack<T>

while (stack.Count > 0) {
    object obj = stack.Pop();
    // ...
    if (SomethingWith(obj)) {
        stack.Push(anotherObj);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM