「毎日Unity」の技術ブログ

開発で役立つ情報を発信する

【UnityC#】Dictionaryの使い方

Dictionaryの使い方を自分用にメモすることにしました。

[ Dictionaryとは ]

DictionaryとはKeyとValueをペアで持つことができるコレクションです。ArrayやListは要素にアクセスする時にIndex値を使いますが、Dictionaryは要素であるValueにアクセスする時にKeyを使います。このKeyの型は自分で好きに指定することができます。もちろんValueの型も自分で好きに指定することができます。

[ 使い方 ]

宣言

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        Dictionary<string, int> DicTest = new Dictionary<string, int>();
    }
}

初期化

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        Dictionary<string, int> DicTest = new Dictionary<string, int>()
        {
            {"a", 1},
            {"b", 2},
            {"c", 3},
        };
    }
}

要素の追加

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        Dictionary<string, int> DicTest = new Dictionary<string, int>();

        DicTest.Add("d", 4);
    }
}

要素の消去

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        Dictionary<string, int> DicTest = new Dictionary<string, int>()
        {
            {"a", 1},
            {"b", 2},
            {"c", 3},
        };

        DicTest.Remove("a");
    }
}

全要素の消去

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        Dictionary<string, int> DicTest = new Dictionary<string, int>()
        {
            {"a", 1},
            {"b", 2},
            {"c", 3},
        };

        DicTest.Clear();
    }
}

ペア数を取得

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        Dictionary<string, int> DicTest = new Dictionary<string, int>()
        {
            {"a", 1},
            {"b", 2},
            {"c", 3},
        };

        Debug.Log(DicTest.Count); // 3
    }
}

KeyからValueを取得

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        Dictionary<string, int> DicTest = new Dictionary<string, int>()
        {
            {"a", 1},
            {"b", 2},
            {"c", 3},
        };

        Debug.Log(DicTest["a"]); // 1
    }
}

ValueからKeyを取得

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        Dictionary<string, int> DicTest = new Dictionary<string, int>()
        {
            {"a", 1},
            {"b", 2},
            {"c", 3},
        };

        Debug.Log(DicTest.FirstOrDefault(a => a.Value == 1).Key); // "a"
    }
}

特定のValueを含むか

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        Dictionary<string, int> DicTest = new Dictionary<string, int>()
        {
            {"a", 1},
            {"b", 2},
            {"c", 3},
        };

        Debug.Log(DicTest.ContainsValue(1)); // true
    }
}

特定のKeyを含むか

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        Dictionary<string, int> DicTest = new Dictionary<string, int>()
        {
            {"a", 1},
            {"b", 2},
            {"c", 3},
        };

        Debug.Log(DicTest.ContainsKey("a")); // true
    }
}

n番目のValueを取得

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        Dictionary<string, int> DicTest = new Dictionary<string, int>()
        {
            {"a", 1},
            {"b", 2},
            {"c", 3},
        };

        Debug.Log(DicTest.Values.ToArray()[0]); // 1
    }
}

n番目のKeyを取得

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        Dictionary<string, int> DicTest = new Dictionary<string, int>()
        {
            {"a", 1},
            {"b", 2},
            {"c", 3},
        };

        Debug.Log(DicTest.Keys.ToArray()[0]); // "a"
    }
}

[ 関連記事 ]

edunity.hatenablog.com