Skip to content

Instantly share code, notes, and snippets.

@zerosalife
Created June 20, 2015 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zerosalife/e3882aa5ffd5dd614c9a to your computer and use it in GitHub Desktop.
Save zerosalife/e3882aa5ffd5dd614c9a to your computer and use it in GitHub Desktop.
Create a Dictionary of Lists in Unity C#
using UnityEngine;
using System.Collections.Generic;
public class MakeDictionaryOfLists : MonoBehaviour {
Dictionary<string, List<int>> table = new Dictionary<string, List<int>>();
void Start() {
// Fill our table with Lists containing ints using Arrays to intialize
// the Lists.
table["a"] = new List<int>(){1};
table["b"] = new List<int>(){2};
table["c"] = new List<int>(){3};
foreach(string str in table.Keys) {
// Get the value for our Key.
List<int> value = table[str];
// If the Key is the desired Key, append to its list.
if(str == "c") {
value.Add(4);
}
// print the first item in each of the Lists.
Debug.Log(value[0]);
}
// Print the appended item to see that it worked.
Debug.Log(table["c"][1]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment