要将队列转换为数组,代码如下-
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..."); foreach(int i in queue){ Console.WriteLine(i); } int[] intArr = queue.ToArray(); Console.WriteLine("Convert Queue to Array..."); foreach(int i in intArr){ Console.WriteLine(i); } } }
输出结果
这将产生以下输出-
Queue... 100 200 300 400 500 600 700 800 900 1000 Convert Queue to Array... 100 200 300 400 500 600 700 800 900 1000
现在让我们来看另一个示例-
using System; using System.Collections.Generic; public class Demo { public static void Main(){ Queue<string> queue = new Queue<string>(); queue.Enqueue("A"); queue.Enqueue("B"); queue.Enqueue("C"); queue.Enqueue("D"); queue.Enqueue("E"); queue.Enqueue("F"); Console.WriteLine("Array..."); foreach(string i in queue){ Console.WriteLine(i); } string[] strArr = queue.ToArray(); Console.WriteLine("Convert Queue to Array..."); foreach(string i in strArr){ Console.WriteLine(i); } } }
输出结果
这将产生以下输出-
Array... A B C D E F Convert Queue to Array... A B C D E F