C# 中的 LinkedList Contains 方法
這是我們的 LinkedList。
int [] num = {1, 3, 7, 15}; LinkedList<int> list = new LinkedList<int>(num);
要檢查列表是否包含某個元素,請使用 Contains() 方法。以下示例在列表中查詢節點 3。
list.Contains(3)
上面返回 True,因為找到元素,如下所示−
示例
using System; using System.Collections.Generic; class Demo { static void Main() { int [] num = {1, 3, 7, 15}; LinkedList<int> list = new LinkedList<int>(num); foreach (var n in list) { Console.WriteLine(n); } // adding a node at the end var newNode = list.AddLast(20); // adding a new node after the node added above list.AddAfter(newNode, 30); Console.WriteLine("LinkedList after adding new nodes..."); foreach (var n in list) { Console.WriteLine(n); } Console.WriteLine("Is number 3 (node) in the list?: "+list.Contains(3)); } }
輸出
1 3 7 15 LinkedList after adding new nodes... 1 3 7 15 20 30 Is number 3 (node) in the list?: True
廣告