কম্পিউটার টিউটোরিয়াল

C#-এ SortedList-র নির্দিষ্ট ইনডেক্সের উপাদান মুছে ফেলা

C#-এ SortedList ক্লাসের RemoveAt(int index) মেথড ব্যবহার করে নির্দিষ্ট ইনডেক্সে অবস্থিত কী-মান জোড়া মুছে ফেলা যায়। SortedList কী অনুযায়ী সর্টেড থাকে, তাই ইনডেক্স ০ হলো সবচেয়ে ছোট কী। RemoveAt ইনডেক্স-বেসড রিমুভ, Remove(key) কী-বেসড রিমুভ।

উদাহরণ ১: ইনডেক্স ৩ (৪র্থ উপাদান) মুছে ফেলা

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

        // ইনডেক্স ৩ (D, 4) মুছে ফেলা
        sortedList.RemoveAt(3);

        Console.WriteLine("\nEnumerator দিয়ে traverser করা হচ্ছে...");
        IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
        while (demoEnum.MoveNext()) {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }
        Console.WriteLine("SortedList কী-মান জোড়ার সংখ্যা (আপডেটেড) = " + sortedList.Count);
    }
}

আউটপুট

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 দিয়ে traverser করা হচ্ছে...
Key = A, Value = 1
Key = B, Value = 2
Key = C, Value = 3
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 কী-মান জোড়ার সংখ্যা (আপডেটেড) = 9

উদাহরণ ২: ইনডেক্স ১ এবং ২ মুছে ফেলা

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("SortedList উপাদান...");
        foreach (DictionaryEntry d in sortedList) {
            Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
        }
        Console.WriteLine("SortedList কী-মান জোড়ার সংখ্যা = " + sortedList.Count);

        // ইনডেক্স ১ (Four, Speakers) এবং ২ (One, Mouse) মুছে ফেলা
        sortedList.RemoveAt(1);
        sortedList.RemoveAt(2);

        Console.WriteLine("\nEnumerator দিয়ে traverser করা হচ্ছে...");
        IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
        while (demoEnum.MoveNext()) {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }
        Console.WriteLine("SortedList কী-মান জোড়ার সংখ্যা (আপডেটেড) = " + sortedList.Count);
    }
}

আউটপুট

SortedList উপাদান...
Key = Five, Value = RAM
Key = Four, Value = Speakers
Key = One, Value = Mouse
Key = Three, Value = Headphone
Key = Two, Value = Keyboard
SortedList কী-মান জোড়ার সংখ্যা = 5
Enumerator দিয়ে traverser করা হচ্ছে...
Key = Five, Value = RAM
Key = One, Value = Mouse
Key = Two, Value = Keyboard
SortedList কী-মান জোড়ার সংখ্যা (আপডেটেড) = 3

ব্যাখ্যা: RemoveAt(index) নির্দিষ্ট ইনডেক্সের এন্ট্রি মুছে ফেলে। ইনডেক্স অবৈধ হলে ArgumentOutOfRangeException থ্রো করে। সর্টেড লিস্ট অ্যারে-বেসড, তাই রিমুভের পর বাকি উপাদানগুলো শিফট করতে হয় (O(n))। ইনডেক্স কী-এর সর্টেড অর্ডার অনুযায়ী। একাধিক রিমুভ করার সময় ইনডেক্স পরিবর্তন হয় বলে সতর্ক থাকতে হয় (উদাহরণ ২-তে দেখানো হয়েছে)। জেনেরিক SortedList<TKey, TValue> ব্যবহার করা ভালো।

  1. C#-তে SortedSet-র সব উপাদান সরাতে Clear() মেথড ব্যবহার

  2. C#-তে SortedSet থেকে নির্দিষ্ট আইটেম সরাতে Remove() মেথড ব্যবহার

  3. C#-তে SortedList-র সব উপাদান সরাতে Clear() মেথড ব্যবহার

  4. পাইথন প্যান্ডাস - CategoricalIndex-এ নতুন ক্যাটেগরি যোগ করুন