當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C# Queue.CopyTo()用法及代碼示例


此方法用於從指定的數組索引開始,將Queue元素複製到現有的一維Array。將元素以枚舉器遍曆隊列的相同順序複製到Array,此方法是O(n)操作,其中n是Count。這種方法屬於System.Collections命名空間。

用法:

public virtual void CopyTo (Array array, int index);

參數:


  • array:一維數組是從Queue複製的元素的目的地。數組必須具有從零開始的索引。
  • index:它是數組中從零開始的索引,複製從該索引開始。

異常:

  • ArgumentNullException:如果數組為null。
  • ArgumentOutOfRangeException:如果索引小於零。
  • ArgumentException:如果數組是多維的Or源隊列中的元素數大於目標數組可以包含的元素數。
  • InvalidCastException:如果源隊列的類型不能自動轉換為目標數組的類型。

以下示例程序旨在說明上述方法的使用:

示例1:

// C# code to illustrate the 
// Queue.CopyTo(Array, Int32) 
// Method 
using System; 
using System.Collections; 
  
class GFG { 
  
    // Driver code 
    public static void Main() 
    { 
  
        // Creating an Queue 
        Queue myq = new Queue(); 
  
        // 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]); 
        } 
    } 
}

輸出:

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

示例2:

// C# code to illustrate the 
// Queue.CopyTo(Array, Int32) 
// Method 
using System; 
using System.Collections; 
  
class GFG { 
  
    // Driver code 
    public static void Main() 
    { 
  
        // Creating an Queue 
        Queue myq = new Queue(); 
  
        // 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[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]); 
        } 
    } 
}

運行時錯誤:

Unhandled Exception:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at (wrapper stelemref) System.Object.virt_stelemref_sealed_class(intptr, object)

參考:



相關用法


注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 Queue.CopyTo() Method in C#。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。