此方法用于将Queue元素复制到新数组。队列未修改,并且新数组中元素的顺序与从队列开始到结束的元素顺序相同。此方法是O(n)运算,属于
句法:
public virtual object[] ToArray ();
返回值:返回一个新数组,其中包含从Queue复制的元素。
下面给出了一些示例,以更好地理解实现:
范例1:
// C# code to illustrate the
// Queue.ToArray Method
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating a Queue
Queue myQueue = new Queue();
// Inserting the elements into the Queue
myQueue.Enqueue("Geeks");
myQueue.Enqueue("Geeks Classes");
myQueue.Enqueue("Noida");
myQueue.Enqueue("Data Structures");
myQueue.Enqueue("GeeksforGeeks");
// Converting the Queue into array
Object[] arr = myQueue.ToArray();
// Displaying the elements in array
foreach(Object ob in arr)
{
Console.WriteLine(ob);
}
}
}
输出:
Geeks
Geeks Classes
Noida
Data Structures
GeeksforGeeks
范例2:
// C# code to illustrate the
// Queue.ToArray Method
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating a Queue
Queue myQueue = new Queue();
// Inserting the elements into the Queue
myQueue.Enqueue(2);
myQueue.Enqueue(3);
myQueue.Enqueue(4);
myQueue.Enqueue(5);
myQueue.Enqueue(6);
// Converting the Queue into array
Object[] arr = myQueue.ToArray();
// Displaying the elements in array
foreach(Object ob in arr)
{
Console.WriteLine(ob);
}
}
}
输出:
2
3
4
5
6
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.queue.toarray?view=netframework-4.7.2