📜  将队列元素复制到C#中的一维数组

📅  最后修改于: 2021-05-29 14:56:14             🧑  作者: Mango

Queue .CopyTo(T [],Int32)方法用于从指定的数组索引开始,将Queue元素复制到现有的一维Array。将元素以枚举器遍历队列的相同顺序复制到Array,此方法是O(n)操作,其中n是Count。此方法位于System.Collections.Generic命名空间下。

句法:

public void CopyTo (T[] array, int arrayIndex);

参数:

例外情况:

  • ArgumentNullException:如果数组为null。
  • ArgumentOutOfRangeException:如果索引小于零。
  • ArgumentException:如果源Queue 中的元素数大于从arrayIndex到目标array末尾的可用空间。

下面的程序说明了上述方法的用法:

范例1:

// 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 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(T[], Int32)
// Method
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating an Queue of String
        Queue myq = new Queue();
  
        // 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]);
        }
    }
}

运行时错误:

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.queue-1.copyto?view=netframework-4.7.2