C#のDictionaryのサンプルです。
目次
- Dictionary<TKey, TValue> クラス
- キーと値を追加する (Addメソッド)
- 要素を初期値でセットする
- キーを指定して値を取得する
- キーの集合を取得する (keysプロパティ)
- 値の集合を取得する(valuesプロパティ)
- 要素数を取得する (Countメソッド)
- 指定のキーがあるかを返す(ContainsKeyメソッド)
- 指定の値があるかを返す(ContainsValueメソッド)
- 要素の位置を指定して要素を削除する (Removeメソッド)
- すべての要素を削除する (Clearメソッド)
Dictionary<TKey, TValue> クラス
- 「キー」と「値」の組み合わせを要素として持ちます。
- 「キー」は一意です。
- キーは任意の文字列を使うことができます。
- サイズをあとから動的に変更できます。
- 以下はMicrosoftのC#のDictionary<TKey, TValue> クラスのリンクです。
https://msdn.microsoft.com/ja-jp/library/xfhwa508(v=vs.110).aspx
キーと値を追加する (Addメソッド)
9行目は、Dictionaryクラスをインスタンス化しています。
代入する変数はvarで宣言できます。8行目のようにデータの型でも可能です。
11-13行目は、Addメソッドでキーと値を追加しています。
15-19行目は、foreach文で値を取得しています。
using System;
using System.Collections.Generic;
class Test1
{
static void Main()
{
//Dictionary<string,string> colors = new Dictionary<string,string>();
var colors = new Dictionary<string,string>();
colors.Add("a","赤");
colors.Add("b","黄");
colors.Add("c","青");
foreach (var a in colors)
{
Console.WriteLine(a.Key);// a b c
Console.WriteLine(a.Value);// 赤 黄 青
}
}
}
要素を初期値でセットする
8-12行目は、Dictionaryクラスをインスタンス化し、かつ初期値をセットしています。
14-18行目は、foreach文で値を取得しています。
using System;
using System.Collections.Generic;
class Test1
{
static void Main()
{
var colors = new Dictionary<string, string>()
{ {"a","赤"},
{"b","黄"},
{"c","青"}
};
foreach (var a in colors)
{
Console.WriteLine(a.Key);// a b c
Console.WriteLine(a.Value);// 赤 黄 青
}
}
}
キーを指定して値を取得する
14行目は、インデクサー([b])で要素の位置を指定して要素を取得しています。
using System;
using System.Collections.Generic;
class Test1
{
static void Main()
{
var colors = new Dictionary<string,string>();
colors.Add("a","赤");
colors.Add("b","黄");
colors.Add("c","青");
Console.WriteLine(colors["b"]);// 黄
}
}
キーの集合を取得する (keysプロパティ)
14行目は、keysプロパティでキーの集合を取得しています。
using System;
using System.Collections.Generic;
class Test1
{
static void Main()
{
var colors = new Dictionary<string,string>();
colors.Add("a","赤");
colors.Add("b","黄");
colors.Add("c","青");
foreach (var a in colors.Keys)
{
Console.WriteLine(a);// a b c
}
}
}
値の集合を取得する(valuesプロパティ)
14行目は、valuesプロパティで値の集合を取得しています。
using System;
using System.Collections.Generic;
class Test1
{
static void Main()
{
var colors = new Dictionary<string,string>();
colors.Add("a","赤");
colors.Add("b","黄");
colors.Add("c","青");
foreach (var a in colors.Values)
{
Console.WriteLine(a);// 赤 黄 青
}
}
}
要素数を取得する (Countメソッド)
14行目は、Countメソッドで要素数を取得しています。
using System;
using System.Collections.Generic;
class Test1
{
static void Main()
{
var colors = new Dictionary<string, string>();
colors.Add("a", "赤");
colors.Add("b", "黄");
colors.Add("c", "青");
Console.WriteLine(colors.Count);// 3
}
}
指定のキーがあるかを返す(ContainsKeyメソッド)
14行目は、ContainsKeyメソッドで指定のキーがあるかを取得しています。
ある場合trueです。
using System;
using System.Collections.Generic;
class Test1
{
static void Main()
{
var colors = new Dictionary<string, string>();
colors.Add("a", "赤");
colors.Add("b", "黄");
colors.Add("c", "青");
Console.WriteLine(colors.ContainsKey("b"));// True
}
}
指定の値があるかを返す(ContainsValueメソッド)
14行目は、ContainsValueメソッドで指定の値があるかを取得しています。
ある場合trueです。
using System;
using System.Collections.Generic;
class Test1
{
static void Main()
{
var colors = new Dictionary<string, string>();
colors.Add("a", "赤");
colors.Add("b", "黄");
colors.Add("c", "青");
Console.WriteLine(colors.ContainsValue("青"));// True
}
}
要素の位置を指定して要素を削除する (Removeメソッド)
14行目は、Removeメソッドで値を指定して削除しています。
using System;
using System.Collections.Generic;
class Test1
{
static void Main()
{
var colors = new Dictionary<string, string>();
colors.Add("a", "赤");
colors.Add("b", "黄");
colors.Add("c", "青");
colors.Remove("b");
foreach (var a in colors)
{
Console.WriteLine(a.Key);// a c
Console.WriteLine(a.Value);// 赤 青
}
}
}
すべての要素を削除する (Clearメソッド)
14行目は、Clearメソッドですべての要素を削除しています。
using System;
using System.Collections.Generic;
class Test1
{
static void Main()
{
var colors = new Dictionary<string, string>();
colors.Add("a", "赤");
colors.Add("b", "黄");
colors.Add("c", "青");
colors.Clear();
Console.WriteLine(colors.Count);// 0
}
}
関連の記事