使用列表
List<int> list = new List<int>();
list.AddRange(otherList);
如何使用队列(Queue)来实现这个功能?因为这个集合(Collection)没有 AddRange 方法。
Queue<int> q = new Queue<int>();
q.AddRange(otherList); //does not exists
使用列表
List<int> list = new List<int>();
list.AddRange(otherList);
如何使用队列(Queue)来实现这个功能?因为这个集合(Collection)没有 AddRange 方法。
Queue<int> q = new Queue<int>();
q.AddRange(otherList); //does not exists
otherList.ForEach(o => q.Enqueue(o));
public static void AddRange<T>(this Queue<T> queue, IEnumerable<T> enu) {
foreach (T obj in enu)
queue.Enqueue(obj);
}
Queue<int> q = new Queue<int>();
q.AddRange(otherList); //Work!
otherList.Foreach(q.Enqueue)
,而无需将其装箱到另一个 lambda 表达式中。 - AdasskoQueue
有一个构造函数,它接受一个ICollection
。您可以将列表传递到队列中,以使用相同的元素进行初始化:
var queue = new Queue<T>(list);
在您的情况下,请按如下方式使用
Queue<int> ques = new Queue<int>(otherList);
Queue<int> q = new Queue<int>(otherList);