C# 您将如何获得队列中的第一个和最后一个项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1308186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:57:42  来源:igfitidea点击:

How would you obtain the first and last items in a Queue?

c#.netvb.netcollectionsqueue

提问by Dan Tao

Say I have a rolling collection of values where I specify the size of the collection and any time a new value is added, any old values beyond this specified size are dropped off. Obviously (and I've tested this) the best type of collection to use for this behavior is a Queue:

假设我有一个滚动的值集合,我在其中指定了集合的大小,并且每当添加新值时,任何超出此指定大小的旧值都会被删除。显然(我已经测试过)用于此行为的最佳集合类型是队列:

myQueue.Enqueue(newValue)
If myQueue.Count > specifiedSize Then myQueue.Dequeue()

However, what if I want to calculate the difference between the first and last items in the Queue? Obviously I can't access the items by index. But to switch from a Queue to something implementing IList seems like overkill, as does writing a new Queue-like class. Right now I've got:

但是,如果我想计算队列中第一个和最后一个项目之间的差异怎么办?显然我无法通过索引访问项目。但是从 Queue 切换到实现 IList 的东西似乎有点矫枉过正,就像编写一个新的类似 Queue 的类一样。现在我有:

Dim firstValue As Integer = myQueue.Peek()
Dim lastValue As Integer = myQueue.ToArray()(myQueue.Count - 1)
Dim diff As Integer = lastValue - firstValue

That call to ToArray()bothers me, but a superior alternative isn't coming to me. Any suggestions?

这个电话让ToArray()我很烦恼,但我没有找到更好的选择。有什么建议?

采纳答案by murgatroid99

One thing you could do is have a temporary variable that stores the value that was just enqueued because that will be the last value and so the variable can be accessed to get that value.

您可以做的一件事是使用一个临时变量来存储刚刚入队的值,因为这将是最后一个值,因此可以访问该变量以获取该值。

回答by Adam Robinson

Your best bet would be to keep track of the last value added to the Queue, then use the myQueue.Peek()function to see the "first" (meaning next) item in the list without removing it.

最好的办法是跟踪添加到 的最后一个值Queue,然后使用该myQueue.Peek()函数查看列表中的“第一个”(意思是下一个)项目而不删除它。

回答by cakeforcerberus

You coulduse a deque (double-ended queue).

可以使用一个deque(double- Ënded队列)。

I don't think there is one built into System.Collections(.Generic) but here's some info on the data structure. If you implemented something like this you could just use PeekLeft() and PeekRight() to get the first and last values.

我不认为 System.Collections(.Generic) 中内置了一个,但这里有一些关于数据结构的信息。如果你实现了这样的东西,你可以只使用 PeekLeft() 和 PeekRight() 来获取第一个和最后一个值。

Of course, it will be up to you whether or not implementing your own deque is preferable to dealing with the unsexiness of ToArray(). :)

当然,实现自己的双端队列是否比处理 ToArray() 的不性感更可取取决于您。:)

http://www.codeproject.com/KB/recipes/deque.aspx

http://www.codeproject.com/KB/recipes/deque.aspx

回答by Juliet

Seems to me if you need quick access to the first item in the list, then you're using the wrong data structure. Switch a LinkedList instead, which conveniently has First and Last properties.

在我看来,如果您需要快速访问列表中的第一项,那么您使用的是错误的数据结构。而是切换一个 LinkedList,它方便地具有 First 和 Last 属性。

Be sure you only add and remove items to the linked list using AddLast and RemoveFirst to maintain the Queue property. To prevent yourself from inadvertantly violating the Queue property, consider creating a wrapper class around the linked list and exposing only the properties you need from your queue.

确保仅使用 AddLast 和 RemoveFirst 向链表添加和删除项目以维护 Queue 属性。为防止您无意中违反 Queue 属性,请考虑围绕链表创建包装类并仅公开您需要的队列属性。

回答by xanadont

public class LastQ<T> : Queue<T>
{
    public T Last { get; private set; }

    public new void Enqueue(T item)
    {
         Last = item;
         base.Enqueue(item);
    }
}

Edit: Obviously this basic class should be more robust to do things like protect the Last property on an empty queue. But this should be enough for the basic idea.

编辑:显然,这个基本类应该更健壮,以执行诸如保护空队列上的 Last 属性之类的操作。但这对于基本思想来说应该足够了。