【发布时间】:2019-09-30 11:07:51
【问题描述】:
我有一个System.Collection.Generic.Queue<int> 和下面的示例代码
Queue<int> iq = new Queue<int>();
iq.Enqueue(1); // 1
iq.Enqueue(2); // 1 2
iq.Enqueue(3); // 1 2 3
//move 1 to the end of the line here
int i = iq.Dequeue(); // 2 3
我想移动值(访问按值)1回到行尾,这样结果是2和1将是最后一个可出列的值。
有什么想法吗?有没有类似iq.MoveToLast(1) 的东西?
【问题讨论】:
-
出队,入队,完成。还是必须是原子的?
-
@Fildor 我需要按值访问。一项要求也可能是将
2移回 -
好的。那是一个不同的故事。然后,您需要一个按索引访问的集合。
-
看来你想要
List<int>而不是Queue<int>:int index = id.IndexOf(2); if (index >= 0) {id.RemoveAt(index); id.Add(2);} -
如果你想保持 Enqueue 和 Dequeue 为“API”,你可以包装一个 List
并相应地实现方法。然后你可以添加一个“MoveToEnd”方法。