Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Xóa phần tử với khóa chỉ định khỏi SortedList trong C#

Giới thiệu

Để xóa một phần tử khỏi SortedList dựa trên khóa, bạn sử dụng phương thức Remove(key). Nếu khóa tồn tại, cặp khóa/giá trị tương ứng bị xóa. Danh sách tự động duy trì thứ tự sắp xếp.

Ví dụ 1: Xóa khóa "H" từ SortedList 10 phần tử

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("Khóa = " + d.Key + ", Giá trị = " + d.Value);
        }
        Console.WriteLine("Số lượng cặp khóa-giá trị = " + sortedList.Count);

        sortedList.Remove("H");

        Console.WriteLine("\nBộ đếm duyệt qua SortedList...");
        IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
        while (demoEnum.MoveNext())
            Console.WriteLine("Khóa = " + demoEnum.Key + ", Giá trị = " + demoEnum.Value);

        Console.WriteLine("Số lượng cặp khóa-giá trị (Đã cập nhật) = " + sortedList.Count);
    }
}

Kết quả

Các phần tử của SortedList...
Khóa = A, Giá trị = 1
Khóa = B, Giá trị = 2
Khóa = C, Giá trị = 3
Khóa = D, Giá trị = 4
Khóa = E, Giá trị = 5
Khóa = F, Giá trị = 6
Khóa = G, Giá trị = 7
Khóa = H, Giá trị = 8
Khóa = I, Giá trị = 9
Khóa = J, Giá trị = 10
Số lượng cặp khóa-giá trị = 10
Bộ đếm duyệt qua SortedList...
Khóa = A, Giá trị = 1
Khóa = B, Giá trị = 2
Khóa = C, Giá trị = 3
Khóa = D, Giá trị = 4
Khóa = E, Giá trị = 5
Khóa = F, Giá trị = 6
Khóa = G, Giá trị = 7
Khóa = I, Giá trị = 9
Khóa = J, Giá trị = 10
Số lượng cặp khóa-giá trị (Đã cập nhật) = 9

Ví dụ 2: Xóa khóa "Three" từ SortedList thiết bị

using System;
using System.Collections;

public class Demo
{
    public static void Main(String[] args)
    {
        SortedList sortedList = new SortedList();
        sortedList.Add("One", "Mouse");
        sortedList.Add("Two", "Keyboard");
        sortedList.Add("Three", "Headphone");
        sortedList.Add("Four", "Speakers");
        sortedList.Add("Five", "RAM");

        Console.WriteLine("Các phần tử của SortedList...");
        foreach (DictionaryEntry d in sortedList)
        {
            Console.WriteLine("Khóa = " + d.Key + ", Giá trị = " + d.Value);
        }
        Console.WriteLine("Số lượng cặp khóa-giá trị = " + sortedList.Count);

        sortedList.Remove("Three");

        Console.WriteLine("\nBộ đếm duyệt qua SortedList...");
        IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
        while (demoEnum.MoveNext())
            Console.WriteLine("Khóa = " + demoEnum.Key + ", Giá trị = " + demoEnum.Value);

        Console.WriteLine("Số lượng cặp khóa-giá trị (Đã cập nhật) = " + sortedList.Count);
    }
}

Kết quả

Các phần tử của SortedList...
Khóa = Five, Giá trị = RAM
Khóa = Four, Giá trị = Speakers
Khóa = One, Giá trị = Mouse
Khóa = Three, Giá trị = Headphone
Khóa = Two, Giá trị = Keyboard
Số lượng cặp khóa-giá trị = 5
Bộ đếm duyệt qua SortedList...
Khóa = Five, Giá trị = RAM
Khóa = Four, Giá trị = Speakers
Khóa = One, Giá trị = Mouse
Khóa = Two, Giá trị = Keyboard
Số lượng cặp khóa-giá trị (Đã cập nhật) = 4

Lưu ý: SortedList sắp xếp theo khóa. Xóa khóa không tồn tại không gây lỗi. Sử dụng RemoveAt(index) nếu muốn xóa theo chỉ số.