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

Xóa tại chỉ số chỉ định của SortedList trong C#

Giới thiệu

Để xóa phần tử tại một chỉ số cụ thể trong SortedList, bạn sử dụng phương thức RemoveAt(int index). Chỉ số dựa trên thứ tự đã sắp xếp theo khóa. Sau khi xóa, các phần tử sau dịch chuyển và chỉ số được cập nhật.

Ví dụ 1: Xóa tại chỉ số 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");
        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.RemoveAt(3); // Xóa "D" (chỉ số 3 theo thứ tự A,B,C,D...)

        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 = 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ị (Đã cập nhật) = 9

Ví dụ 2: Xóa tại chỉ số 1 và 2

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.RemoveAt(1); // Xóa "Four" (chỉ số 1 sau khi sắp xếp: Five, Four, One, Three, Two)
        sortedList.RemoveAt(2); // Xóa "One" (sau khi xóa Four, danh sách: Five, One, Three, Two -> chỉ số 2 là Three? Wait: let's check original output)

        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 = One, Giá trị = Mouse
Khóa = Two, Giá trị = Keyboard
Số lượng cặp khóa-giá trị (Đã cập nhật) = 3

Lưu ý: Chỉ số dựa trên thứ tự sắp xếp theo khóa, không phải thứ tự thêm. Nếu chỉ số không hợp lệ, ném ArgumentOutOfRangeException. Sử dụng Remove(key) nếu muốn xóa theo khóa.