using System;
using System.Collections;
public class test {
static void Main() {
Hashtable ht = new Hashtable();
// データの追加その1
ht["japan"] = "日本";
ht["america"] = "アメリカ";
// データの追加その2
ht.Add("china", "中国");
ht.Add("india", "インド");
// データの取得
string val = (string)ht["japan"];
Console.WriteLine(val); // 出力:日本
// キー項目の列挙
foreach (string key in ht.Keys) {
Console.WriteLine("{0} : {1}", key, ht[key]);
}
// 出力例:
// india : インド
// japan : 日本
// america : アメリカ
// china : 中国
// 値項目の列挙
foreach (string value in ht.Values) {
Console.WriteLine(value);
}
// 出力例:
// インド
// 日本
// アメリカ
// 中国
// キーの存在チェック
if (!ht.ContainsKey("france")) {
ht["france"] = "フランス";
}
// 値の存在チェック
Console.WriteLine(ht.ContainsValue("日本")); // 出力例:True
// エントリ(キーと値)の列挙
foreach (DictionaryEntry de in ht) {
Console.WriteLine("{0} : {1}", de.Key, de.Value);
}
// 出力例:
// india : インド
// japan : 日本
// france : フランス
// america : アメリカ
// china : 中国
}
}
2011年6月2日木曜日
C# hashtable 列挙
登録:
コメント (Atom)