Computer >> 컴퓨터 >  >> 프로그래밍 >> C#

C# SortedList에서 특정 값의 인덱스 구하기 – IndexOfValue() 메서드 활용법


SortedList에서 지정된 값의 인덱스 구하기

C#의 SortedList 개체에서 특정 값(Value)이 위치한 인덱스를 알아내려면 IndexOfValue() 메서드를 사용하면 됩니다. 이 메서드는 해당 값이 처음 발견되는 0 기반(zero-based) 인덱스를 반환하며, 목록에 그 값이 존재하지 않으면 -1을 반환합니다.

참고로 SortedList는 키(Key)를 기준으로 항상 오름차순으로 자동 정렬되므로, 요소를 추가한 순서와 실제 출력 순서는 서로 다를 수 있다는 점을 유의해야 합니다.

예제 1

다음은 두 개의 SortedList를 만들고, 각각 IndexOfValue() 메서드로 특정 값의 인덱스를 조회하는 예제입니다.

using System;
using System.Collections;
public class Demo {
   public static void Main(String[] args) {
      SortedList list1 = new SortedList();
      list1.Add("One", 1);
      list1.Add("Two ", 2);
      list1.Add("Three ", 3);
      list1.Add("Four", 4);
      list1.Add("Five", 5);
      list1.Add("Six", 6);
      list1.Add("Seven ", 7);
      list1.Add("Eight ", 8);
      list1.Add("Nine", 9);
      list1.Add("Ten", 10);
      Console.WriteLine("SortedList1 elements...");
      foreach(DictionaryEntry d in list1) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.Write("Index at value 7 = "+list1.IndexOfValue(7));
      SortedList list2 = new SortedList();
      list2.Add("A", "Accessories");
      list2.Add("B", "Books");
      list2.Add("C", "Smart Wearable Tech");
      list2.Add("D", "Home Appliances");
      Console.WriteLine("\n\nSortedList2 elements...");
      foreach(DictionaryEntry d in list2) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.Write("Index at value Books = "+list2.IndexOfValue("Books"));
   }
}

출력 결과

위 코드를 실행하면 다음과 같은 결과가 출력됩니다.

SortedList1 elements...
Eight 8
Five 5
Four 4
Nine 9
One 1
Seven 7
Six 6
Ten 10
Three 3
Two 2
Index at value 7 = 5
SortedList2 elements...
A Accessories
B Books
C Smart Wearable Tech
D Home Appliances
Index at value Books = 1

첫 번째 목록(list1)은 키가 알파벳순(Eight, Five, Four, Nine, One…)으로 정렬되어 있으며, 값 7은 인덱스 5에 위치함을 확인할 수 있습니다. 두 번째 목록(list2)에서는 값 "Books"가 인덱스 1에 있음을 알 수 있습니다.

예제 2

이번에는 부서 정보를 담은 SortedList에서 여러 값의 인덱스를 한꺼번에 조회하는 예제를 살펴보겠습니다.

using System;
using System.Collections;
public class Demo {
   public static void Main(String[] args) {
      SortedList list = new SortedList();
      list.Add("One", "Finance");
      list.Add("Two", "Marketing");
      list.Add("Three", "Sales");
      list.Add("Four", "Purchase");
      list.Add("Five", "Operations");
      list.Add("Six", "IT");
      Console.WriteLine("SortedList elements...");
      foreach(DictionaryEntry d in list) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      Console.WriteLine("\nIndex at value Marketing = "+list.IndexOfValue("Marketing"));
      Console.WriteLine("Index at value Purchase = "+list.IndexOfValue("Purchase"));
      Console.WriteLine("Index at value Sales = "+list.IndexOfValue("Sales"));
      Console.WriteLine("Index at value IT = "+list.IndexOfValue("IT"));
      Console.WriteLine("Index at value Operations = "+list.IndexOfValue("Operations"));
   }
}

출력 결과

위 코드를 실행하면 다음과 같은 결과가 출력됩니다.

SortedList elements...
Five Operations
Four Purchase
One Finance
Six IT
Three Sales
Two Marketing
Index at value Marketing = 5
Index at value Purchase = 1
Index at value Sales = 4
Index at value IT = 3
Index at value Operations = 0

정리 및 참고 사항

IndexOfValue()는 값을 기준으로 인덱스를 찾는 반면, 키를 기준으로 인덱스를 구하고 싶다면 IndexOfKey() 메서드를 사용하면 됩니다. 또한 동일한 값이 여러 개 존재할 경우 IndexOfValue()가장 먼저 일치하는 항목의 인덱스만 반환한다는 점도 기억해 두시기 바랍니다.