Để sao chép toàn bộ LinkedList sang Mảng, mã như sau -
Ví dụ
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);
}
}
} Đầu ra
Điều này sẽ tạo ra kết quả sau -
100 200 300 0 0
Ví dụ
Bây giờ chúng ta hãy xem một ví dụ khác -
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);
}
}
} Đầu ra
Điều này sẽ tạo ra kết quả sau -
0 0 0 0 100 200 300 0 0 0