要获取LinkedList中包含的节点数,代码如下-
using System; using System.Collections.Generic; public class Demo { public static void Main() { LinkedList<String> list = new LinkedList<String>(); list.AddLast("A"); list.AddLast("B"); list.AddLast("C"); list.AddLast("D"); list.AddLast("E"); list.AddLast("F"); list.AddLast("G"); list.AddLast("H"); list.AddLast("I"); list.AddLast("J"); Console.WriteLine("Count of nodes = " + list.Count); Console.WriteLine("First Node = "+list.First.Value); list.Clear(); Console.WriteLine("节点数(已更新) = " + list.Count); } }
输出结果
这将产生以下输出-
Count of nodes = 10 First Node = A 节点数(已更新) = 0
让我们看另一个例子-
using System; using System.Collections.Generic; public class Demo { public static void Main(String[] args) { LinkedList<String> list1 = new LinkedList<String>(); list1.AddLast("One"); list1.AddLast("Two"); list1.AddLast("Three"); list1.AddLast("Four"); list1.AddLast("Five"); Console.WriteLine("LinkedList1..."); foreach (string res in list1) { Console.WriteLine(res); } LinkedList<String> list2 = new LinkedList<String>(); list2.AddLast("India"); list2.AddLast("US"); list2.AddLast("UK"); list2.AddLast("Canada"); list2.AddLast("Poland"); list2.AddLast("Netherlands"); Console.WriteLine("LinkedList2..."); foreach (string res in list2) { Console.WriteLine(res); } LinkedList<String> list3 = new LinkedList<String>(); Console.WriteLine("LinkedList3 节点数 = "+list3.Count); list3 = list2; Console.WriteLine("LinkedList3 节点数 (已更新) = "+list2.Count); Console.WriteLine("LinkedList3是否等于LinkedList2? = "+list3.Equals(list2)); } }
输出结果
这将产生以下输出-
LinkedList1... One Two Three Four Five LinkedList2... India US UK Canada Poland Netherlands LinkedList3 节点数 = 0 LinkedList3 节点数 (已更新) = 6 LinkedList3是否等于LinkedList2? = True