要从另一个集合创建一个队列,代码如下-
using System; using System.Collections.Generic; public class Demo { public static void Main() { Queue<string> queue = new Queue<string>(); queue.Enqueue("One"); queue.Enqueue("Two"); queue.Enqueue("Three"); Console.WriteLine("Queue elements..."); foreach(string str in queue) { Console.WriteLine(str); } Console.WriteLine("\nArray elements..."); Queue<string> arr = new Queue<string>(queue.ToArray()); foreach(string str in arr) { Console.WriteLine(str); } } }
输出结果
这将产生以下输出-
Queue elements... One Two Three Array elements... One Two Three
现在让我们来看另一个示例-
using System; using System.Collections.Generic; public class Demo { public static void Main() { Queue<int> queue = new Queue<int>(); queue.Enqueue(100); queue.Enqueue(200); queue.Enqueue(300); queue.Enqueue(400); queue.Enqueue(500); queue.Enqueue(600); queue.Enqueue(700); queue.Enqueue(800); queue.Enqueue(900); queue.Enqueue(1000); Console.WriteLine("Queue elements..."); foreach(int val in queue) { Console.WriteLine(val); } Console.WriteLine("\nArray elements..."); Queue<int> arr = new Queue<int>(queue.ToArray()); foreach(int val in arr) { Console.WriteLine(val); } } }
输出结果
这将产生以下输出-
Queue elements... 100 200 300 400 500 600 700 800 900 1000 Array elements... 100 200 300 400 500 600 700 800 900 1000