Phương thức Stack<T>.CopyTo() trong C# được dùng để sao chép các phần tử của Stack vào một mảng một chiều (Array) hiện có, bắt đầu từ chỉ mục được chỉ định.
Cú pháp
public virtual void CopyTo(T[] array, int arrayIndex);
array: Mảng một chiều đích (phải đủ lớn).arrayIndex: Chỉ mục từ 0 trong mảng đích nơi bắt đầu sao chép.
Ví dụ 1: Stack<int> CopyTo mảng int[]
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
Stack stack = new Stack();
stack.Push(150);
stack.Push(300);
stack.Push(500);
stack.Push(750);
stack.Push(1000);
stack.Push(1250);
stack.Push(1500);
stack.Push(2000);
stack.Push(2500);
Console.WriteLine("Các phần tử Stack (top -> bottom):");
foreach (int val in stack)
{
Console.WriteLine(val);
}
Console.WriteLine("Số lượng phần tử = " + stack.Count);
Console.WriteLine("Stack chứa 750? = " + stack.Contains(750));
stack.Push(3000);
Console.WriteLine("\nStack sau khi Push 3000...");
foreach (int val in stack)
{
Console.WriteLine(val);
}
Console.WriteLine("Số lượng (cập nhật) = " + stack.Count);
// Clone Stack
Stack stack2 = new Stack(stack); // Copy constructor
Console.WriteLine("\nStack cloned...");
foreach (int val in stack2)
{
Console.WriteLine(val);
}
Console.WriteLine("Số lượng cloned = " + stack2.Count);
// CopyTo mảng
Console.WriteLine("\nSao chép cloned stack sang mảng int[]...");
int[] intArr = new int[stack2.Count];
stack2.CopyTo(intArr, 0);
foreach (int j in intArr)
{
Console.WriteLine(j);
}
}
}
Kết quả
Các phần tử Stack (top -> bottom): 2500 2000 1500 1250 1000 750 500 300 150 Số lượng phần tử = 9 Stack chứa 750? = True Stack sau khi Push 3000... 3000 2500 2000 1500 1250 1000 750 500 300 150 Số lượng (cập nhật) = 10 Stack cloned... 3000 2500 2000 1500 1250 1000 750 500 300 150 Số lượng cloned = 10 Sao chép cloned stack sang mảng int[]... 3000 2500 2000 1500 1250 1000 750 500 300 150
Ví dụ 2: Stack<string> CopyTo mảng string[]
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
Stack stack = new Stack();
stack.Push("Projectors");
stack.Push("Monitors");
stack.Push("XPS");
stack.Push("Laptop");
stack.Push("Notebook");
Console.WriteLine("Các phần tử Stack...");
foreach (string val in stack)
{
Console.WriteLine(val);
}
Console.WriteLine("Số lượng = " + stack.Count);
Console.WriteLine("Chứa XPS? = " + stack.Contains("XPS"));
stack.Push("Ultrabook");
Console.WriteLine("\nStack sau Push Ultrabook...");
foreach (string val in stack)
{
Console.WriteLine(val);
}
Console.WriteLine("Số lượng (cập nhật) = " + stack.Count);
Stack stack2 = new Stack(stack);
Console.WriteLine("\nStack cloned...");
foreach (string val in stack2)
{
Console.WriteLine(val);
}
Console.WriteLine("Số lượng cloned = " + stack2.Count);
Console.WriteLine("\nSao chép cloned stack sang mảng string[]...");
string[] strArr = new string[stack2.Count];
stack2.CopyTo(strArr, 0);
foreach (string j in strArr)
{
Console.WriteLine(j);
}
}
}
Kết quả
Các phần tử Stack... Notebook Laptop XPS Monitors Projectors Số lượng = 5 Chứa XPS? = True Stack sau Push Ultrabook... Ultrabook Notebook Laptop XPS Monitors Projectors Số lượng (cập nhật) = 6 Chứa Ultrabook? = True Stack cloned... Ultrabook Notebook Laptop XPS Monitors Projectors Số lượng cloned = 6 Sao chép cloned stack sang mảng string[]... Ultrabook Notebook Laptop XPS Monitors Projectors
Thứ tự phần tử khi CopyTo
Stack là LIFO (Last-In-First-Out). Khi duyệt foreach hoặc CopyTo, thứ tự là top -> bottom (phần tử Push gần nhất đầu tiên).
array[0]= Top của Stackarray[Count-1]= Bottom của Stack
Lưu ý
- Mảng đích phải đủ lớn:
array.Length - arrayIndex >= stack.Count, ngược lại némArgumentException. arrayIndexphải >= 0.- Phương thức không generic
Stack.CopyTo(Array, int)dùng cho non-generic Stack. - Dùng
new Stack<T>(existingStack)hoặcToArray()để clone nhanh hơn.