ภาพรวม
LinkedList<T>.CopyTo(T[] array, int index) คัดลอกข้อมูลจาก LinkedList ไปยังอาร์เรย์ ตามลำดับจากหัว (head) ไปหาง (tail)
ตัวอย่างที่ 1: คัดลอก 3 สมาชิกไปอาร์เรย์ขนาด 5 เริ่มดัชนี 0
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
LinkedList<int> list = new LinkedList<int>();
list.AddLast(100);
list.AddLast(200);
list.AddLast(300);
int[] strArr = new int[5];
list.CopyTo(strArr, 0);
foreach (int str in strArr)
{
Console.WriteLine(str);
}
}
}
ผลลัพธ์
100 200 300 0 0
ตัวอย่างที่ 2: คัดลอก 3 สมาชิกไปอาร์เรย์ขนาด 10 เริ่มดัชนี 4
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
LinkedList<int> list = new LinkedList<int>();
list.AddLast(100);
list.AddLast(200);
list.AddLast(300);
int[] strArr = new int[10];
list.CopyTo(strArr, 4);
foreach (int str in strArr)
{
Console.WriteLine(str);
}
}
}
ผลลัพธ์
0 0 0 0 100 200 300 0 0 0
สรุป
- ลำดับข้อมูลในอาร์เรย์จะตรงกับลำดับใน LinkedList (head ไป tail)
- อาร์เรย์ปลายทางต้องมีขนาด >= Count + index
- ช่องว่างจะเป็นค่า default (0 สำหรับ int)