Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

ดึง Enumerator เพื่อวนลูปผ่าน SortedDictionary ใน C#

SortedDictionary<TKey, TValue> เรียงลำดับคีย์อัตโนมัติ และให้เมธอด GetEnumerator() คืนค่า IDictionaryEnumerator สำหรับการวนลูปผ่านคู่คีย์-ค่า

ตัวอย่างที่ 1: SortedDictionary<int, string>

using System;
using System.Collections;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        SortedDictionary<int, string> sortedDict = new SortedDictionary<int, string>();
        sortedDict.Add(100, "Mobile");
        sortedDict.Add(200, "Laptop");
        sortedDict.Add(300, "Desktop");
        sortedDict.Add(400, "Speakers");
        sortedDict.Add(500, "Headphone");
        sortedDict.Add(600, "Earphone");

        Console.WriteLine("SortedDictionary คู่คีย์-ค่า...");
        
        // ใช้ IDictionaryEnumerator
        IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
        {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }
    }
}

ผลลัพธ์ (เรียงตามคีย์จากน้อยไปมาก)

SortedDictionary คู่คีย์-ค่า...
Key = 100, Value = Mobile
Key = 200, Value = Laptop
Key = 300, Value = Desktop
Key = 400, Value = Speakers
Key = 500, Value = Headphone
Key = 600, Value = Earphone

ตัวอย่างที่ 2: SortedDictionary<int, int> ขนาดใหญ่กว่า

using System;
using System.Collections;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        SortedDictionary<int, int> sortedDict = new SortedDictionary<int, int>();
        sortedDict.Add(100, 1);
        sortedDict.Add(200, 2);
        sortedDict.Add(300, 3);
        sortedDict.Add(400, 4);
        sortedDict.Add(500, 5);
        sortedDict.Add(600, 6);
        sortedDict.Add(700, 7);
        sortedDict.Add(800, 8);
        sortedDict.Add(900, 9);
        sortedDict.Add(1000, 10);

        Console.WriteLine("SortedDictionary คู่คีย์-ค่า...");
        IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
        {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }
    }
}

ผลลัพธ์

SortedDictionary คู่คีย์-ค่า...
Key = 100, Value = 1
Key = 200, Value = 2
Key = 300, Value = 3
Key = 400, Value = 4
Key = 500, Value = 5
Key = 600, Value = 6
Key = 700, Value = 7
Key = 800, Value = 8
Key = 900, Value = 9
Key = 1000, Value = 10

วิธีการวนลูปแบบสมัยใหม่ (แนะนำ)

// 1. ใช้ foreach กับ KeyValuePair (แนะนำมากที่สุด)
foreach (var kvp in sortedDict)
{
    Console.WriteLine($"Key = {kvp.Key}, Value = {kvp.Value}");
}

// 2. ใช้ foreach กับ Keys หรือ Values แยกกัน
foreach (var key in sortedDict.Keys)
{
    Console.WriteLine($"Key: {key}, Value: {sortedDict[key]}");
}

// 3. ใช้ LINQ
sortedDict.ToList().ForEach(kvp => Console.WriteLine($"{kvp.Key}: {kvp.Value}"));

ข้อสังเกตสำคัญ

  • SortedDictionary ใช้ Red-Black Tree ให้การค้นหา แทรก ลบ ที่ O(log n)
  • คีย์ต้องไม่ซ้ำกัน (ArgumentException หากเพิ่มคีย์ซ้ำ)
  • GetEnumerator() คืนค่า IDictionaryEnumerator (legacy) แต่แนะนำให้ใช้ IEnumerable<KeyValuePair<TKey, TValue>> ผ่าน foreach แทน
  • ต่างจาก SortedList ที่ใช้ Array แบบเรียงลำดับ (หน่วยความจำน้อยกว่า แต่อินเซิร์ตช้ากว่า)