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);
}
// adding a node at the end
var newNode = list.AddLast("Emma");
// adding a new node after the node added above
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