Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

การคัดลอกสมาชิก Queue ไปยังอาร์เรย์ 1 มิติใน C#


เมธอด Queue<T>.CopyTo(T[], Int32) ใช้สำหรับคัดลอกสมาชิกใน Queue ไปยังอาร์เรย์ 1 มิติ

ตัวอย่าง

using System; 
using System.Collections.Generic; 

public class Demo{ 
    public static void Main(){ 
        Queue<string> queue = new Queue<string>(); 
        queue.Enqueue("K"); 
        queue.Enqueue("T"); 
        String[] strArr = new String[4]; 
        strArr[0] = "One"; 
        strArr[1] = "Two"; 
        strArr[2] = "Three"; 
        strArr[3] = "Four"; 
        Console.WriteLine("\nArray elements: "); 
        for (int i = 0; i < strArr.Length; i++){ 
            Console.WriteLine(strArr[i]); 
        } 
        queue.CopyTo(strArr, 2); 
        Console.WriteLine("\nAfter copying array contains: "); 
        for (int i = 0; i < strArr.Length; i++){ 
            Console.WriteLine("arr[{0}] : {1}", i, strArr[i]); 
        } 
    } 
}

ผลลัพธ์

One 
Two 
Three 
Four 
After copying array contains: 
arr[0] : One 
arr[1] : Two 
arr[2] : K 
arr[3] : T