使用该Contains()
方法检查节点是否为LinkedList。
这是我们的LinkedList。
string [] students = {"Beth","Jennifer","Amy","Vera"}; LinkedList<string> list = new LinkedList<string>(students);
现在,要检查节点“ Amy”是否在列表中,我们将使用Contains()
如下所示的方法-
list.Contains("Amy")
在这种情况下,该方法将返回一个布尔值,即True。
让我们看完整的代码。
using System; using System.Collections.Generic; class Demo { static void Main() { string [] students = {"Beth","Jennifer","Amy","Vera"}; LinkedList<string> list = new LinkedList<string>(students); foreach (var stu in list) { Console.WriteLine(stu); } //在最后添加一个节点 var newNode = list.AddLast("Emma"); //在上面添加的节点之后添加一个新节点 list.AddAfter(newNode, "Matt"); Console.WriteLine("LinkedList after adding new nodes..."); foreach (var stu in list) { Console.WriteLine(stu); } Console.WriteLine("Is Student Amy (node) in the list?: "+list.Contains("Amy")); Console.WriteLine("Is Student Anne (node) in the list?: "+list.Contains("Anne")); } }
输出结果
Beth Jennifer Amy Vera LinkedList after adding new nodes... Beth Jennifer Amy Vera Emma Matt Is Student Amy (node) in the list?: True Is Student Anne (node) in the list?: False