[英]How would I implement a Queue of type Dictionary<int, string> and iterate/enqueue/dequeue in C#
I am looking to implement a Queue
of type Dictionary<int, string>
and be able to iterate/enqueue/dequeue. 我期待实现类型Dictionary<int, string>
的Queue
,并能够迭代/入队/出队。
What's ultimately needed is a queue
of int
, string
, whatever guise it takes. 最终需要的是一个int
, string
的queue
,无论它需要什么样的伪装。
So far I have something like: 到目前为止我有类似的东西:
private static Queue<Dictionary<int, string>> requestQueue = new Queue<Dictionary<int, string>>();
foreach (KeyValuePair<int, string> dictionaryListItem in dictionaryList)
{
requestQueue.Enqueue( dictionaryListItem ); // error
}
but can't seem to enqueue with the above. 但似乎无法与上述人员一起排队。 Would anyone know the correct syntax? 谁会知道正确的语法?
Well, you have a queue of dictionaries, but try to add a single dictionary value to your queue. 好吧,你有一个字典队列,但尝试在队列中添加一个字典值。
If you indeed want to have a queue of dictionaries, you should change your code like this: 如果你确实想要一个字典队列,你应该改变你的代码:
requestQueue.Enqueue(dictionaryList);
If you actually want a queue of key value pairs, change your queue to this: 如果您确实需要键值对队列,请将队列更改为:
Queue<KeyValuePair<int, string>> requestQueue
Simple snippet: 简单片段:
Queue<KeyValuePair<int, string>> queue = new Queue<KeyValuePair<int, string>>();
void Enqueue()
{
queue.Enqueue(new KeyValuePair<int, string>(1, "One"));
queue.Enqueue(new KeyValuePair<int, string>(2, "Two"));
//..
}
void Dequeue()
{
while (true)
{
var kvp = queue.Dequeue();
Console.WriteLine(string.Format("key: {0}, value: {1}", kvp.Key, kvp.Value));
}
//..
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.