This 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
namespace.
Syntax:
public virtual void CopyTo (Array array, int index);
Parameters:
array: It is the one-dimensional Array that is the destination of the elements copied from Queue. The Array must have zero-based indexing.
index: 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 array is multidimensional
Or
the number of elements in the source Queue is greater than the number of elements that the destination array can contain. - InvalidCastException: If the type of the source Queue cannot be cast automatically to the type of the destination array.
Below programs illustrate the use of above-discussed method:
Example 1:
using System;
using System.Collections;
class GFG {
public static void Main()
{
Queue myq = new Queue();
myq.Enqueue( "A" );
myq.Enqueue( "B" );
myq.Enqueue( "C" );
myq.Enqueue( "D" );
String[] arr = new String[6];
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: " );
foreach (Object obj in myq)
{
Console.WriteLine(obj);
}
Console.WriteLine( "\nArray Contains: " );
for ( int i = 0; i < arr.Length; i++) {
Console.WriteLine( "arr[{0}] : {1}" , i, arr[i]);
}
Console.WriteLine( "After Method: " );
myq.CopyTo(arr, 2);
Console.WriteLine( "\nQueue Contains: " );
foreach (Object obj in myq)
{
Console.WriteLine(obj);
}
Console.WriteLine( "\nArray Contains: " );
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:
using System;
using System.Collections;
class GFG {
public static void Main()
{
Queue myq = new Queue();
myq.Enqueue( "A" );
myq.Enqueue( "B" );
myq.Enqueue( "C" );
myq.Enqueue( "D" );
String[] arr = new String[2];
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: " );
foreach (Object obj in myq)
{
Console.WriteLine(obj);
}
Console.WriteLine( "\nArray Contains: " );
for ( int i = 0; i < arr.Length; i++) {
Console.WriteLine( "arr[{0}] : {1}" , i, arr[i]);
}
Console.WriteLine( "After Method: " );
myq.CopyTo(arr, 2);
Console.WriteLine( "\nQueue Contains: " );
foreach (Object obj in myq)
{
Console.WriteLine(obj);
}
Console.WriteLine( "\nArray Contains: " );
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.
at (wrapper stelemref) System.Object.virt_stelemref_sealed_class(intptr, object)
Reference: