C#のSortedListで指定したインデックスの要素を削除する方法(RemoveAtメソッド)
C#のSortedListコレクションから、指定されたインデックス位置にある要素を削除するには、RemoveAt()メソッドを使用します。この記事では、RemoveAtメソッドの基本的な使い方を、実際のコード例と実行結果とともにわかりやすく解説します。
RemoveAt()メソッドとは
RemoveAt(int index)は、SortedList内の指定されたインデックス(0から始まる番号)にあるキーと値のペアを削除するメソッドです。削除後は、後続の要素が自動的に前に詰められ、Countプロパティの値も1つ減少します。
サンプルコード1:単一の要素を削除する
まずは、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");
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 elements...");
foreach(DictionaryEntry d in sortedList) {
Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value);
}
Console.WriteLine("Count of SortedList key-value pairs = "+sortedList.Count);
// インデックス3の要素を削除
sortedList.RemoveAt(3);
Console.WriteLine("\nEnumerator to iterate through the SortedList...");
IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
Console.WriteLine("Count of SortedList key-value pairs (Updated) = "+sortedList.Count);
}
}実行結果
上記のコードを実行すると、次のような出力が得られます。インデックス3にあった「Key = D」の要素が削除されていることが確認できます。
SortedList elements... 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 Count of SortedList key-value pairs = 10 Enumerator to iterate through the SortedList... 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 Count of SortedList key-value pairs (Updated) = 9
サンプルコード2:複数の要素を連続して削除する
次に、RemoveAtメソッドを複数回呼び出して、複数の要素を削除する例を見てみましょう。
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 elements...");
foreach(DictionaryEntry d in sortedList) {
Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value);
}
Console.WriteLine("Count of SortedList key-value pairs = "+sortedList.Count);
// インデックス1と2の要素を順に削除
sortedList.RemoveAt(1);
sortedList.RemoveAt(2);
Console.WriteLine("\nEnumerator to iterate through the SortedList...");
IDictionaryEnumerator demoEnum = sortedList.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
Console.WriteLine("Count of SortedList key-value pairs (Updated) = "+sortedList.Count);
}
}実行結果
このコードでは、SortedListはキーによって自動的にソートされるため、「Five」「Four」「One」「Three」「Two」の順に並んでいます。ここでインデックス1(Four)を削除すると、残りの要素が詰められ、次にインデックス2(Three)が削除されます。最終的に「Five」「One」「Two」の3つの要素だけが残ります。
SortedList elements... Key = Five, Value = RAM Key = Four, Value = Speakers Key = One, Value = Mouse Key = Three, Value = Headphone Key = Two, Value = Keyboard Count of SortedList key-value pairs = 5 Enumerator to iterate through the SortedList... Key = Five, Value = RAM Key = One, Value = Mouse Key = Two, Value = Keyboard Count of SortedList key-value pairs (Updated) = 3
注意点
- SortedListはキーに基づいて自動的にソートされるため、追加した順序とインデックスの順序は一致しない場合があります。削除する前に、どの要素がどのインデックスに配置されているかを確認しましょう。
- 存在しないインデックスを指定すると、
ArgumentOutOfRangeExceptionがスローされます。事前にCountプロパティで要素数をチェックすることをおすすめします。 - キーを指定して削除したい場合は、
Remove(object key)メソッドを使用します。
まとめ
SortedListから指定したインデックスの要素を削除するには、RemoveAt()メソッドを使います。削除後は要素が自動的に詰められ、要素数も更新されます。SortedListはキーでソートされる特性があるため、インデックスと実際の要素の対応関係に注意しながら操作することが重要です。
-
C#のSortedListからすべての要素を削除する方法
C#のSortedListからすべての要素を一括で削除するには、Clear()メソッドを使用します。このメソッドを呼び出すと、SortedListに格納されているすべてのキーと値のペアが削除され、Countプロパティの値は0になります。例1:Clear()メソッドで全要素を削除する以下のコードでは、10個のキーと値のペアを持つSortedListを作成し、Clear()メソッドですべての要素を削除しています。using System;using System.Collections;public class Demo { public static void Main(
-
Python Pandas入門:CategoricalIndexから指定したカテゴリを削除する方法
CategoricalIndexから特定のカテゴリを削除するには、Pandasのremove_categories()メソッドを使用します。このメソッドを使うと、指定したカテゴリがインデックスから取り除かれ、それらのカテゴリに属していた値は自動的にNaN(欠損値)に置き換えられます。必要なライブラリのインポートまず、必要なライブラリをインポートします。import pandas as pdCategoricalIndexの作成「categories」パラメータを使用してカテゴリカルデータのカテゴリを設定し、「ordered」パラメータを使用して順序付きのカテゴリカルとして扱います。catInd