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

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

SortedList ให้เมธอด GetEnumerator() คืนค่า IDictionaryEnumerator สำหรับวนลูปคู่คีย์-ค่าที่เรียงลำดับตามคีย์แล้ว

ตัวอย่างที่ 1: SortedList 10 รายการ

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 องค์ประกอบ...");
        foreach (DictionaryEntry d in sortedList)
        {
            Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
        }

        Console.WriteLine("จำนวนคู่คีย์-ค่าใน SortedList = " + sortedList.Count); // 10

        Console.WriteLine("\nEnumerator วนลูปผ่าน SortedList...");
        IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
        while (demoEnum.MoveNext())
        {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }
    }
}

ผลลัพธ์ (เรียงตามคีย์ A-J)

SortedList องค์ประกอบ...
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
จำนวนคู่คีย์-ค่าใน SortedList = 10
Enumerator วนลูปผ่าน SortedList...
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

ตัวอย่างที่ 2: 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("Enumerator วนลูปผ่าน SortedList...");
        IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
        while (demoEnum.MoveNext())
        {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }
    }
}

ผลลัพธ์ (เรียงตามอักษร: Eight, Five, Four...)

Enumerator วนลูปผ่าน 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

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

// 1. ใช้ foreach กับ DictionaryEntry (Non-Generic)
foreach (DictionaryEntry entry in sortedList)
{
    Console.WriteLine($"Key: {entry.Key}, Value: {entry.Value}");
}

// 2. แปลงเป็น Generic SortedList<TKey, TValue> (แนะนำ)
var genericSortedList = new SortedList<string, string>();
genericSortedList.Add("A", "1");
// ...
foreach (var kvp in genericSortedList)
{
    Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}");
}

// 3. ใช้ Keys/Values แยกกัน
foreach (var key in sortedList.Keys)
{
    Console.WriteLine($"Key: {key}, Value: {sortedList[key]}");
}

// 4. LINQ
sortedList.Cast<DictionaryEntry>().ToList().ForEach(e => Console.WriteLine($"{e.Key}: {e.Value}"));

SortedList vs SortedDictionary

คุณสมบัติSortedListSortedDictionary
โครงสร้าง2 Array (Keys, Values) เรียงแล้วRed-Black Tree
MemoryCompact (น้อยกว่า)มากกว่า (Node/Pointer)
LookupO(log n) Binary SearchO(log n) Tree
Insert/DeleteO(n) - ต้อง Shift ArrayO(log n)
Get by Indexรองรับ (GetKey, GetByIndex)ไม่รองรับโดยตรง
GenericSortedList<TKey, TValue>SortedDictionary<TKey, TValue>

ข้อสังเกต

  • Non-generic SortedList ใช้ object แนะนำใช้ SortedList<TKey, TValue>
  • Enumerator คืนค่าตามลำดับคีย์ที่เรียงแล้วเสมอ
  • เหมาะสำหรับข้อมูลที่เพิ่ม/ลบน้อย แต่อ่านมาก (Lookup Heavy)
  • คีย์ต้อง Implement IComparable หรือระบุ IComparer