Copying the Queue elements to 1D Array in C# (original) (raw)
Last Updated : 28 Jan, 2019
Queue.CopyTo(T[], Int32) Method is used to copy the Queue elements to an existing one-dimensional Array, starting at the specified array index. The elements are copied to the Array in the same order in which the enumerator iterates through the Queue and this method is an O(n) operation, where n is Count. This method comes under System.Collections.Generic
namespace.Syntax:
public void CopyTo (T[] array, int arrayIndex);
Parameters:
array: It is the one-dimensional Array which is the destination of the elements copied from Queue. The Array must have zero-based indexing.arrayIndex: It is the zero-based index in array at which copying begins.
Exceptions:
- ArgumentNullException: If the array is null.
- ArgumentOutOfRangeException: If the index is less than zero.
- ArgumentException: If the number of elements in the source Queue is greater than the available space from arrayIndex to the end of the destination array.
Below programs illustrate the use of the above-discussed method:Example 1:
csharp `
// C# code to illustrate the // Queue.CopyTo(T[], Int32) // Method using System; using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Creating an Queue of String
Queue<string> myq = new Queue<string>();
// Adding elements to Queue
myq.Enqueue("A");
myq.Enqueue("B");
myq.Enqueue("C");
myq.Enqueue("D");
// Creates and initializes the
// one-dimensional target Array.
String[] arr = new String[6];
// adding elements to Array
arr[0] = "HTML";
arr[1] = "PHP";
arr[2] = "Java";
arr[3] = "Python";
arr[4] = "C#";
arr[5] = "OS";
Console.WriteLine("Before Method: ");
Console.WriteLine("\nQueue Contains: ");
// Displaying the elements in myq
foreach(Object obj in myq)
{
Console.WriteLine(obj);
}
Console.WriteLine("\nArray Contains: ");
// Displaying the elements in arr
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine("arr[{0}] : {1}", i, arr[i]);
}
Console.WriteLine("After Method: ");
// Copying the entire source Queue
// to the target Array starting at
// index 2.
myq.CopyTo(arr, 2);
Console.WriteLine("\nQueue Contains: ");
// Displaying the elements in myq
foreach(Object obj in myq)
{
Console.WriteLine(obj);
}
Console.WriteLine("\nArray Contains: ");
// Displaying the elements in arr
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine("arr[{0}] : {1}", i, arr[i]);
}
}
}
`
Output:
Before Method:
Queue Contains: A B C D
Array Contains: arr[0] : HTML arr[1] : PHP arr[2] : Java arr[3] : Python arr[4] : C# arr[5] : OS After Method:
Queue Contains: A B C D
Array Contains: arr[0] : HTML arr[1] : PHP arr[2] : A arr[3] : B arr[4] : C arr[5] : D
Example 2:
csharp `
// C# code to illustrate the // Queue.CopyTo(T[], Int32) // Method using System; using System.Collections.Generic;
class GFG {
// Driver code
public static void Main()
{
// Creating an Queue of String
Queue<string> myq = new Queue<string>();
// Adding elements to Queue
myq.Enqueue("GFG");
myq.Enqueue("Geeks");
myq.Enqueue("Sudo");
myq.Enqueue("DSA");
// Creates and initializes the
// one-dimensional target Array.
String[] arr = new String[2];
// adding elements to Array
arr[0] = "HTML";
arr[1] = "PHP";
arr[2] = "Java";
arr[3] = "Python";
arr[4] = "C#";
arr[5] = "OS";
Console.WriteLine("Before Method: ");
Console.WriteLine("\nQueue Contains: ");
// Displaying the elements in myq
foreach(Object obj in myq)
{
Console.WriteLine(obj);
}
Console.WriteLine("\nArray Contains: ");
// Displaying the elements in arr
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine("arr[{0}] : {1}", i, arr[i]);
}
Console.WriteLine("After Method: ");
// using Method but It will give
// Runtime Error as number of elements
// in the source Queue is greater
// than the number of elements that
// the destination array can contain
myq.CopyTo(arr, 2);
Console.WriteLine("\nQueue Contains: ");
// Displaying the elements in myq
foreach(Object obj in myq)
{
Console.WriteLine(obj);
}
Console.WriteLine("\nArray Contains: ");
// Displaying the elements in arr
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine("arr[{0}] : {1}", i, arr[i]);
}
}
}
`
Runtime Error:
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
Reference:
Similar Reads
- C# | Copying the elements of ArrayList to a new array ArrayList.ToArray Method is used to copy the elements of the ArrayList to a new array. This method contains two methods in its overload list as follows: ToArray()ToArray(Type)ToArray() This method is used to copy the elements of the ArrayList to a new Object array. The elements are copied using Arra 2 min read
- C# | Copying the Hashtable elements to an Array Instance Hashtable.CopyTo(Array, Int32) Method is used to copy the elements of a Hashtable to a one-dimensional Array instance at the specified index.Syntax: public virtual void CopyTo (Array array, int arrayIndex); Parameters: array : The one-dimensional Array that is the destination of the DictionaryEntry 3 min read
- C# | Copying the SortedList elements to an Array Object SortedList.CopyTo(Array, Int32) Method is used to copy SortedList elements to a one-dimensional Array object, starting at the specified index in the array. Syntax: public virtual void CopyTo (Array array, int arrayIndex); Parameters: array: It is the one-dimensional Array object that is the destinat 2 min read
- C# | Copying the Collection elements to an array Collection.CopyTo(T[], Int32) method is used to copy the entire Collection to a compatible one-dimensional Array, starting at the specified index of the target array. Syntax: public void CopyTo (T[] array, int index); Parameters: array : The one-dimensional Array that is the destin 3 min read
- C# | Copying BitArray elements to an Array The BitArray class manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on i.e, 1 and false indicates the bit is off i.e, 0. This class is contained in System.Collections namespace. BitArray.CopyTo(Array, Int32) method is used to copy the ent 3 min read
- C# | Convert Queue To array Queue represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called deque. Queue.ToArray Method is used to copy the Queue elements to a new array 2 min read
- C# | Get the number of elements contained in the Queue Queue represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called deque. Queue.Count Property is used to get the number of elements contained i 2 min read
- C# | Get the number of elements actually contained in the ArrayList ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Count property gets the number of elements actually contained in 3 min read
- C# | Check if an element is in the Queue Queue represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called deque. Queue.Contains(T) Method is used to check whether an element is in the 2 min read
- C# | Copying the HybridDictionary entries to an Array Instance HybridDictionary.CopyTo(Array, Int32) method is used to copy the HybridDictionary entries to a one-dimensional Array instance at the specified index. Syntax: public void CopyTo (Array array, int index); Parameters: array : The one-dimensional Array that is the destination of the DictionaryEntry obje 3 min read