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

ลบองค์ประกอบทั้งหมดจาก SortedList ใน C#

เมธอด Clear() ของ SortedList ใช้ลบคู่คีย์-ค่าทั้งหมด ทำให้ Count กลายเป็น 0 คีย์จะถูกเก็บในลำดับเรียงเสมอ

ตัวอย่างที่ 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

        // ลบทั้งหมด
        sortedList.Clear();

        Console.WriteLine("จำนวนคู่คีย์-ค่าใน SortedList (อัปเดต) = " + sortedList.Count); // 0
    }
}

ผลลัพธ์ (เรียงตามคีย์ 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
จำนวนคู่คีย์-ค่าใน SortedList (อัปเดต) = 0

ตัวอย่างที่ 2: SortedList 3 รายการ

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");

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

        Console.WriteLine("SortedList องค์ประกอบ...");
        foreach (DictionaryEntry d in sortedList)
        {
            Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
        }

        sortedList.Clear();

        Console.WriteLine("จำนวนคู่คีย์-ค่าใน SortedList (อัปเดต) = " + sortedList.Count); // 0
    }
}

ผลลัพธ์

จำนวนคู่คีย์-ค่าใน SortedList = 3
SortedList องค์ประกอบ...
Key = A, Value = 1
Key = B, Value = 2
Key = C, Value = 3
จำนวนคู่คีย์-ค่าใน SortedList (อัปเดต) = 0

SortedList vs SortedDictionary vs Dictionary

คุณสมบัติSortedListSortedDictionaryDictionary
โครงสร้างข้อมูลArray แบบเรียง (2 Array: keys, values)Red-Black TreeHash Table
ลำดับเรียงตาม Keyเรียงตาม Keyไม่รับประกัน
ค้นหา (Lookup)O(log n) - Binary SearchO(log n) - TreeO(1) - Hash
แทรก/ลบO(n) - ต้อง Shift ArrayO(log n)O(1) Average
หน่วยความจำน้อยกว่า (Compact)มากกว่า (Node/Pointer)ปานกลาง
Generic VersionSortedList<TKey, TValue>SortedDictionary<TKey, TValue>Dictionary<TKey, TValue>

ข้อสังเกต

  • SortedList (Non-generic) ใช้ object แนะนำใช้ SortedList<TKey, TValue>
  • Clear() ทำให้ Count = 0 แต่ Capacity (ขนาด Array ภายใน) อาจคงอยู่
  • ใช้ TrimToSize() เพื่อลด Capacity ให้เท่ากับ Count (Non-generic) หรือ TrimExcess() (Generic)
  • คีย์ต้องไม่ซ้ำ และไม่เป็น null