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

C#에서 SortedList에 포함된 요소 개수 구하기

C#에서 SortedList에 포함된 요소의 개수를 확인하려면 Count 속성을 사용합니다. Count 속성은 SortedList에 실제로 저장되어 있는 키-값 쌍의 총 개수를 정수 형태로 반환합니다.

예제 1

다음 예제는 SortedList에 여러 개의 키-값 쌍을 추가한 후 Count 속성으로 개수를 출력하고, Clear() 메서드를 호출해 모든 요소를 제거한 뒤 개수가 0으로 변경되는 과정을 보여줍니다.

using System;
using System.Collections;
public class Demo {
   public static void Main(String[] args){
      SortedList sortedList = new SortedList();
      sortedList.Add("A", "1");
      sortedList.Add("B", "2");
      sortedList.Add("C", "3");
      sortedList.Add("D", "4");
      sortedList.Add("E", "5");
      sortedList.Add("F", "6");
      sortedList.Add("G", "7");
      sortedList.Add("H", "8");
      sortedList.Add("I", "9");
      sortedList.Add("J", "10");
      Console.WriteLine("SortedList elements...");
      foreach(DictionaryEntry d in sortedList){
         Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value);
      }
      Console.WriteLine("Count of SortedList key-value pairs = "+sortedList.Count);
      sortedList.Clear();
      Console.WriteLine("Count of SortedList (updated) = "+sortedList.Count);
   }
}

출력 결과

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

SortedList elements...
Key = A, Value = 1
Key = B, Value = 2
Key = C, Value = 3
Key = D, Value = 4
Key = E, Value = 5
Key = F, Value = 6
Key = G, Value = 7
Key = H, Value = 8
Key = I, Value = 9
Key = J, Value = 10
Count of SortedList key-value pairs = 10
Count of SortedList (updated) = 0

예제 2

이번에는 IDictionaryEnumerator를 사용해 SortedList의 각 요소를 순회하면서 Count 속성 값을 함께 확인하는 예제입니다. SortedList는 키를 기준으로 자동 정렬되므로, 요소가 영문 알파벳 순서대로 출력되는 점도 확인할 수 있습니다.

using System;
using System.Collections;
public class Demo {
   public static void Main(String[] args){
      SortedList sortedList = new SortedList();
      sortedList.Add("One", "1");
      sortedList.Add("Two", "2");
      sortedList.Add("Three", "3");
      sortedList.Add("Four", "4");
      sortedList.Add("Five", "5");
      sortedList.Add("Six", "6");
      sortedList.Add("Seven", "7");
      sortedList.Add("Eight", "8");
      sortedList.Add("Nine", "9");
      sortedList.Add("Ten", "10");
      Console.WriteLine("Count of SortedList key-value pairs = "+sortedList.Count);
      Console.WriteLine("Enumerator to iterate through the SortedList...");
      IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
      while (demoEnum.MoveNext())
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
   }
}

출력 결과

실행 결과는 다음과 같습니다.

Count of SortedList key-value pairs = 10
Enumerator to iterate through the SortedList...
Key = Eight, Value = 8
Key = Five, Value = 5
Key = Four, Value = 4
Key = Nine, Value = 9
Key = One, Value = 1
Key = Seven, Value = 7
Key = Six, Value = 6
Key = Ten, Value = 10
Key = Three, Value = 3
Key = Two, Value = 2

정리

SortedList의 Count 속성은 컬렉션에 포함된 실제 요소(키-값 쌍)의 개수를 나타냅니다. Clear() 메서드를 호출하면 모든 요소가 제거되어 Count 값이 0이 되며, GetEnumerator() 메서드로 얻은 IDictionaryEnumerator를 사용하면 SortedList의 요소를 키 정렬 순서대로 순회할 수 있습니다.