C#でStringDictionaryからすべてのエントリを削除する方法
C#のStringDictionaryからすべてのエントリ(キーと値のペア)を一括で削除するには、Clear()メソッドを使用します。このメソッドを呼び出すと、コレクション内の全要素が削除され、Countプロパティの値は0になります。
なお、StringDictionaryではキーは自動的に小文字に変換されて格納される点にも注目してください。以下に具体的なコード例を示します。
例1:2つのStringDictionaryを比較してClear()を確認する
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
StringDictionary strDict1 = new StringDictionary();
strDict1.Add("A", "John");
strDict1.Add("B", "Andy");
strDict1.Add("C", "Tim");
strDict1.Add("D", "Ryan");
strDict1.Add("E", "Kevin");
strDict1.Add("F", "Katie");
strDict1.Add("G", "Brad");
Console.WriteLine("StringDictionary1 elements...");
foreach(DictionaryEntry d in strDict1){
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Does StringDictionary1 has key G? "+strDict1.ContainsKey("G"));
StringDictionary strDict2 = new StringDictionary();
strDict2.Add("A", "John");
strDict2.Add("B", "Andy");
strDict2.Add("C", "Tim");
strDict2.Add("D", "Ryan");
strDict2.Add("E", "Kevin");
strDict2.Add("F", "Katie");
strDict2.Add("G", "Brad");
Console.WriteLine("\nStringDictionary2 elements...");
foreach(DictionaryEntry d in strDict2){
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Count of key/value pairs in StringDictionary2 = " + strDict2.Count);
strDict2.Clear();
Console.WriteLine("Count of key/value pairs in StringDictionary2 (updated) = " + strDict2.Count);
}
}出力
上記のコードを実行すると、次のような出力が得られます。
StringDictionary1 elements... a John b Andy c Tim d Ryan e Kevin f Katie g Brad Does StringDictionary1 has key G? True StringDictionary2 elements... a John b Andy c Tim d Ryan e Kevin f Katie g Brad Count of key/value pairs in StringDictionary2 = 7 Count of key/value pairs in StringDictionary2 (updated) = 0
この例では、strDict2に対してClear()メソッドを呼び出した直後にCountを表示しており、要素数が7から0に変化していることが確認できます。一方、strDict1にはContainsKey()メソッドで特定のキー("G")が存在するかどうかをチェックしています。
例2:シンプルなStringDictionaryのクリア操作
続いて、もう少しシンプルな例を見てみましょう。
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
StringDictionary strDict = new StringDictionary();
strDict.Add("A", "John");
strDict.Add("B", "Andy");
strDict.Add("C", "Tim");
strDict.Add("D", "Ryan");
Console.WriteLine("StringDictionary elements...");
foreach(DictionaryEntry d in strDict){
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("Count of key/value pairs in StringDictionary = " + strDict.Count);
strDict.Clear();
Console.WriteLine("Count of key/value pairs in StringDictionary (updated) = " + strDict.Count);
}
}出力
上記のコードを実行すると、次のような出力が得られます。
StringDictionary elements... a John b Andy c Tim d Ryan Count of key/value pairs in StringDictionary = 4 Count of key/value pairs in StringDictionary (updated) = 0
まとめ
StringDictionaryのすべてのエントリを削除したい場合は、Clear()メソッドを呼び出すだけで簡単に実現できます。個々の要素をRemove()メソッドで削除する場合と異なり、Clear()はコレクション全体を一度に空にできるため、効率的です。削除後もStringDictionaryオブジェクト自体はそのまま使えるため、新しい要素を追加し直すことも可能です。
-
JavaでArrayListのすべての要素を削除する方法(clearメソッドの使い方)
JavaのArrayListからすべての要素を一括で削除したい場合は、clear()メソッドを使用します。この記事では、実際のコード例を通じて、ArrayListの全要素を削除する手順をわかりやすく解説します。1. 要素を持つArrayListを作成するまず、複数の要素を格納したArrayListを用意しましょう。以下の例では、Integer型のArrayListに7つの数値を追加しています。ArrayList<Integer> arrlist = new ArrayList<Integer>(5); arrlist.add(25); arrlist.add(50);
-
Google Chromeのオートフィル(自動入力)情報を削除する3つの方法
Google Chromeには、保存しておいた情報をもとにフォームを自動入力する「オートフィル(自動入力)」機能が搭載されています。対象となるのは、検索キーワードやURL、ユーザー名、住所、パスワード、クレジットカードなどの支払い情報です。新しい情報をフォームに入力すると、Chromeが「この情報を保存しますか?」と確認してくることもあります。しかし、家族や同僚とパソコンを共有している場合など、オートフィル情報が残ったままだと都合が悪いケースもあります。この記事では、Google Chromeからオートフィル情報を削除するための具体的な手順をわかりやすく解説します。特定のオートフィル候補だけを