Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Phương thức Queue.CopyTo() trong C#

Giới thiệu

Phương thức Queue.CopyTo() trong C# sao chép các phần tử của Queue vào một mảng một chiều (one-dimensional Array) đã tồn tại, bắt đầu từ chỉ số mảng chỉ định.

Cú pháp

public virtual void CopyTo (Array array, int index);
  • array: mảng một chiều đích, phải có đủ không gian từ index đến cuối.
  • index: chỉ số 0-based trong mảng đích nơi bắt đầu sao chép.

Ví dụ 1: Copy Queue<int> sang mảng int[]

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        Queue queue = new Queue();
        queue.Enqueue(100);
        queue.Enqueue(200);
        queue.Enqueue(300);

        Console.Write("Số lượng phần tử = ");
        Console.WriteLine(queue.Count);

        Console.WriteLine("Queue...");
        foreach(int item in queue) {
            Console.WriteLine(item);
        }

        Console.WriteLine("Queue có chứa 500? = " + queue.Contains(500));

        int[] intArr = new int[5];
        intArr[0] = 1;
        intArr[1] = 2;
        intArr[2] = 3;
        intArr[3] = 4;
        // intArr[4] = 0 (mặc định)

        queue.CopyTo(intArr, 1); // Sao chép từ index 1

        Console.WriteLine("\nQueue (giữ nguyên)");
        foreach(int item in queue) {
            Console.WriteLine(item);
        }

        Console.WriteLine("\nMảng (đã cập nhật)");
        foreach(int item in intArr) {
            Console.WriteLine(item);
        }
    }
}

Đầu ra ví dụ 1

Số lượng phần tử = 3
Queue...
100
200
300
Queue có chứa 500? = False

Queue (giữ nguyên)
100
200
300

Mảng (đã cập nhật)
1
100
200
300
0

Ví dụ 2: Copy Queue<string> sang mảng string[]

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        Queue queue = new Queue();
        queue.Enqueue("Tim");
        queue.Enqueue("Jack");
        queue.Enqueue("Nathan");
        queue.Enqueue("Tom");
        queue.Enqueue("David");
        queue.Enqueue("Mark");

        Console.Write("Số lượng phần tử = ");
        Console.WriteLine(queue.Count);

        Console.WriteLine("Queue...");
        foreach(string item in queue) {
            Console.WriteLine(item);
        }

        string[] strArr = new string[10];
        strArr[0] = "AB";
        strArr[1] = "BC";
        strArr[2] = "DE";
        strArr[3] = "EF";
        // strArr[4..9] = null (mặc định)

        queue.CopyTo(strArr, 1); // Sao chép từ index 1

        Console.WriteLine("\nQueue (giữ nguyên)");
        foreach(string item in queue) {
            Console.WriteLine(item);
        }

        Console.WriteLine("\nMảng (đã cập nhật)");
        foreach(string item in strArr) {
            Console.WriteLine(item);
        }
    }
}

Đầu ra ví dụ 2

Số lượng phần tử = 6
Queue...
Tim
Jack
Nathan
Tom
David
Mark

Queue (giữ nguyên)
Tim
Jack
Nathan
Tom
David
Mark

Mảng (đã cập nhật)
AB
Tim
Jack
Nathan
Tom
David
Mark
(null)
(null)
(null)

Lưu ý quan trọng

  • Queue không bị thay đổi sau khi CopyTo.
  • Thứ tự sao chép: từ đầu queue (front) đến cuối (rear) → mảng[index], mảng[index+1]...
  • Nếu index < 0 hoặc index + queue.Count > array.Length → ném ArgumentException.
  • Mảng phải là một chiều (rank = 1).
  • Với Queue<T> generic, mảng đích phải là T[] hoặc object[] (covariance cho reference type).

Ứng dụng thực tế

  • Chuyển queue sang mảng để dùng Array.Sort, Array.BinarySearch, LINQ.
  • Snapshot trạng thái queue tại một thời điểm (debug, logging).
  • Batch processing: copy sang mảng, xử lý batch, xóa queue.
  • Interop với API yêu cầu mảng (legacy, P/Invoke).

So sánh CopyTo vs ToArray

Phương thứcTạo mảng mới?Cho phép offset?Trả về
CopyTo(Array, index)Không (mảng có sẵn)Cóvoid
ToArray()Có (mảng mới)Không (luôn từ 0)T[] / object[]