Để xóa tất cả các cặp key-value khỏi SortedList trong C#, bạn sử dụng phương thức Clear(). SortedList duy trì các phần tử được sắp xếp theo key theo thứ tự tăng dần.
Ví dụ 1: Xóa tất cả từ SortedList lớn
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("Các phần tử của SortedList...");
foreach (DictionaryEntry d in sortedList)
{
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
Console.WriteLine("Số cặp key-value của SortedList = " + sortedList.Count);
// Xóa tất cả phần tử
sortedList.Clear();
Console.WriteLine("Số cặp key-value của SortedList (đã cập nhật) = " + sortedList.Count);
}
}
Kết quả
Các phần tử của 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
Số cặp key-value của SortedList = 10
Số cặp key-value của SortedList (đã cập nhật) = 0
Ví dụ 2: SortedList nhỏ
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("Số cặp key-value của SortedList = " + sortedList.Count);
Console.WriteLine("Các phần tử của SortedList...");
foreach (DictionaryEntry d in sortedList)
{
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
sortedList.Clear();
Console.WriteLine("Số cặp key-value của SortedList (đã cập nhật) = " + sortedList.Count);
}
}
Kết quả
Số cặp key-value của SortedList = 3
Các phần tử của SortedList...
Key = A, Value = 1
Key = B, Value = 2
Key = C, Value = 3
Số cặp key-value của SortedList (đã cập nhật) = 0
Đặc điểm của SortedList
| Đặc điểm |
Mô tả |
| Sắp xếp |
Tự động sắp xếp key tăng dần |
| Cấu trúc |
2 mảng song song (keys[] và values[]) |
| Truy xuất index |
Hỗ trợ GetKey(index), GetByIndex(index) |
| Key trùng |
Không cho phép |
| Key null |
Không cho phép |
| Phức tạp Clear() |
O(n) |
SortedList vs SortedDictionary
| Đặc điểm |
SortedList |
SortedDictionary |
| Cấu trúc |
Mảng (Array-based) |
Cây đỏ-đen (Tree-based) |
| Bộ nhớ |
Ít hơn (compact) |
Nhiều hơn (node objects) |
| Insert/Remove |
O(n) - dịch chuyển mảng |
O(log n) - cân bằng cây |
| Truy xuất index |
O(1) - hỗ trợ trực tiếp |
Không hỗ trợ |
| Phù hợp |
Dữ liệu ít thay đổi, cần index |
Dữ liệu thường thêm/xóa |
Generic Version: SortedList<TKey, TValue>
using System.Collections.Generic;
SortedList<string, int> sortedList = new SortedList<string, int>();
sortedList.Add("A", 1);
sortedList.Add("B", 2);
sortedList.Add("C", 3);
Console.WriteLine("Count: " + sortedList.Count); // 3
sortedList.Clear();
Console.WriteLine("Count: " + sortedList.Count); // 0
// Type-safe, hiệu năng tốt hơn (không boxing/unboxing)
Lưu ý
Clear() không giải phóng bộ nhớ mảng nội bộ (Capacity không đổi)
- Để giải phóng bộ nhớ:
sortedList.TrimExcess() (chỉ generic) hoặc gán null
- Non-generic
SortedList trong System.Collections
- Generic
SortedList<TKey, TValue> trong System.Collections.Generic
- Key phải implement
IComparable<TKey> hoặc cung cấp IComparer<TKey>