Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Queue.ToArray Method in C#
The Queue<T>.ToArray() method in C# copies all Queue elements to a new array. This method creates a shallow copy of the queue's elements in the same order (FIFO - First In, First Out).
Syntax
The syntax for the generic Queue<T> is as follows −
public T[] ToArray();
Return Value
Returns a new array of type T[] containing copies of the elements from the Queue. The array elements maintain the same order as they would be dequeued from the queue.
Using ToArray() with Integer Queue
Example
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Queue<int> queue = new Queue<int>();
queue.Enqueue(1);
queue.Enqueue(2);
queue.Enqueue(3);
queue.Enqueue(4);
queue.Enqueue(5);
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);
}
Console.WriteLine("Original queue count: " + queue.Count);
Console.WriteLine("Array length: " + intArr.Length);
}
}
The output of the above code is −
Queue... 1 2 3 4 5 Convert Queue to Array... 1 2 3 4 5 Original queue count: 5 Array length: 5
Using ToArray() with String Queue
Example
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("Queue...");
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);
}
// Demonstrate that ToArray() creates a copy
Console.WriteLine("Modifying array doesn't affect queue:");
strArr[0] = "Modified";
Console.WriteLine("First queue element: " + queue.Peek());
Console.WriteLine("First array element: " + strArr[0]);
}
}
The output of the above code is −
Queue... A B C D E F Convert Queue to Array... A B C D E F Modifying array doesn't affect queue: First queue element: A First array element: Modified
Key Points
-
The
ToArray()method creates a new array ? modifications to the array do not affect the original queue. -
Elements are copied in FIFO order ? the first element enqueued becomes the first array element.
-
The original queue remains unchanged after calling
ToArray(). -
Time complexity is O(n) where n is the number of elements in the queue.
Conclusion
The Queue<T>.ToArray() method provides an efficient way to convert queue elements into an array format while preserving FIFO order. This method creates a separate copy, ensuring the original queue remains unmodified and can be useful for scenarios requiring array-based operations on queue data.
