To copy the entire LinkedList to Array, the code is as follows −
Example
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);
}
}
}Output
This will produce the following output −
100 200 300 0 0
Example
Let us now see another example −
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);
}
}
}Output
This will produce the following output −
0 0 0 0 100 200 300 0 0 0