This is the sixth day of my participation in the August More text Challenge. For details, see:August is more challenging
Dictionaries and lists nested within each other)
📢 preface
- Recently, the nesting of lists and dictionaries has been required for work
- I forgot how to use it for a long time, so I searched it
- It is found that there are articles about the use of nesting, but very scattered, not complete
- So I wrote an article, and I tried it out with my own code examples, and I came up with just about every way to nest dictionaries and lists
- Let’s learn how to use nested dictionaries and lists.
🏳 ️ 🌈 dictionary
Dictionary meaning: In a dictionary, the key can only correspond to one value but not multiple values.
- Dic =new Dictionary< key, value> ();
Dictionary<string.string> Dic=new Dictionary<string.string> ();// Create instantiation normally
Dictionary<string.string> Dic1 = new Dictionary<string.string> {{"a"."1" }, { "b"."2"}};// Create the instantiation directly and assign the value
Copy the code
- Common methods:
Add: di.add (key, value) Adds a value to the Dictionary. Delete: di.remove (key) Removes the specified value. Access: Dictionary[key] specifies the value of the key. Dictionary traversal methods:
Dictionary<string.string> d = new Dictionary<string.string> ();foreach (var item in d)
{
Console.WriteLine(item.Key+item.Value);// Iterate over the values in the printed list
}
Copy the code
Dictionaries nest dictionaries
- Dictionary nesting dictionary. When creating a dictionary, write another dictionary on the inside layer to complete the dictionary nesting. The inside dictionary is equivalent to the Value of the outer dictionary
- When instantiating, only the outer dictionary is instantiated. When assigning a value to the outer dictionary, the inner nested dictionary needs to be instantiated
- Then, after getting the key of the outer dictionary, we assign key and Value to the inner dictionary
- When iterating, a double-layer loop, first iterating over the outer dictionary, then iterating over the values of the outer dictionary (i.e., the inner dictionary)
Dictionary<string, Dictionary<string.string>> Dic1;
Dic1 = new Dictionary<string, Dictionary<string.string> > ();/ / method
Dic1.Add("key".new Dictionary<string.string> ());// The corresponding embedded dictionary needs to be instantiated
Dic1["key"].Add("key"."value");// Add the outer value
/ / method 2
Dic1["key"] = new Dictionary<string.string> ();// Assign a dictionary value to one of the outer values
Dic1["key"].Add("key1"."value");// The key of the outer dictionary is assigned to the value of the nested dictionary
Console.WriteLine(Dic1["key"] ["key1"]);// Read a value from a nested dictionary
// Print the result: value
// Iterate over nested dictionaries
foreach (var item in Dic1)
{
Debug.Log("Outer dictionary:" + item);// Iterates to print the values in the outer dictionary
foreach (var item1 in item.Value)
{
Console.WriteLine("Inner dictionary:" + item1);// Print the values in the inner dictionary by iterating over the values in the outer dictionary}}// Print the result:
/ / outer Dictionary: keySystem. Collections. Generic. The Dictionary ` 2 [System. String, System. String]
// Inner dictionary: [key1, value]
Copy the code
Dictionaries nest lists
- The nested dictionary list is the same as the nested dictionary above
- Also replace the Value of the outer dictionary with a list
- And then when you instantiate, you instantiate the outer dictionary first, and then you instantiate the list elements when you assign to the dictionary
- It’s exactly the same as the dictionary nested dictionary when traversing
Dictionary<string, List<string>> Dic1;
Dic1 = new Dictionary<string, List<string> > (); Dic1.Add("key1".new List<string> { "The first."."The second."."The third." });// Assign values to dictionaries and lists
Dic1.Add("key2".new List<string> { "Fourth."."The fifth"."The sixth" });// Assign values to dictionaries and lists
foreach (var item in Dic1)
{
Console.WriteLine("Dictionary:" + item.Key + item.Value);// Iterate over the values in the print dictionary
foreach (var item1 in item.Value)
{
Console.WriteLine("List:"+item1);// Iterate over the values in the printed list}}Copy the code
🏳 ️ 🌈 list
When we have a lot of data of the same type, we can use an array to store and manage the data, but this disadvantage is that the size of the array is given in advance and is fixed.
- If we have a lot of data of the same type but a variable amount, we can use collection classes to manage it — for example, List. We can use List to easily add data, delete data and other data operations.
- Three ways to instantiate
methods1: List <int> list = new List<int> ();// Create an empty listmethods2:var list = new List<int> ();// Create an empty listmethods3: Creates and initializes an assignmentvar list = new List<int> {1.2.3}; Csharp List<int> scoreList = new List<int> ();// Take each element in the list, assign it to Temp, and execute the body of the loop
foreach (string s in list)
{
Console.WriteLine(s);// Iterate over the values in the printed list
// Print the result: list 2 list 3
}
// The second type: traverses all indexes and accesses the elements in the list through the indexes
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i]);// Iterate over the values of the printed list
}
Copy the code
- Method of use
Add: list.add (key) Adds a value to the dictionary. Delete: list.remove (key) Removes the specified value. Access: list[key] indicates the value corresponding to the key
- List trivia
(1) The list internal data is actually stored in an array. An empty list will have an array of length zero inside it. When an element is added to the list, the size of the list increases to 4, when a fifth element is added, the size of the list increases again to 8, and so on. It increases by a factor of two. (2) When the size of the list changes, it creates a new Array and uses the array.copy () method to Copy the elements from the Array into the new Array. To save time, if you know in advance how many elements you want to store, you can use the list constructor to specify the size of the list
Such as:
List<int> intList = new List<int> (10);
// Create a list with capacity 10
// When the capacity is insufficient, the capacity will be doubled each time
Copy the code
We can use the Capacity property to get and set the Capacity size.
intList.Capacity = 100;
Copy the code
Note the difference between the capacity and the number of elements in the list.
Capacity is the length of the array used to store data in the list and is obtained by Capacity. The elements in the list are the data that we add and need to manage, and we get them through Count.
List nested list
- The nested list is relatively easy to understand, because when we add a list, we only need to add one property value
- For nesting it would be List
-
, and then add the inner List as a value to the outer List
- When traversal is also a double-layer loop access
// Create nested lists and plain lists
List<List<string>> list1 = new List<List<string> > (); List<string> list2 = new List<string> (); List<string> list3 = new List<string> ();// Common list assignment
list2.Add("The list 2");
list3.Add("Chain table 3");
// Nested list assignment
list1.Add(list2);
list1.Add(list3);
// Traverse the nested list
foreach (List<string> i in list1)
{
foreach (string s in i)
{
Console.WriteLine(s);// Print the list value
// Print the result: list 2 list 3}}Copy the code
List nested dictionary
- List nested dictionary, which treats the dictionary as an attribute value of the outer list
- The dictionary is then added to the list using the Add method
- When you iterate, you iterate over the list first, and then you iterate over the dictionary to get the data
List<Dictionary<string.string>> list1;// Create a list of nested dictionaries
Dictionary<string.string> d;// Create a dictionary
d = new Dictionary<string.string> ();// Instantiate the dictionary
list1 = new List<Dictionary<string.string> > ();// Instantiate the list
list1.Add(d);// Add the dictionary to the list
d.Add("key"."value");// Assign a value directly to the dictionary
list1[1].Add("key"."value");// You can also assign a value to a dictionary via a list
// Iterate over nested dictionaries
foreach (var item in list1)
{
foreach (var item1 in item)
{
Console.WriteLine(item1);// Print the value of the dictionary}}Copy the code
👥 summary
There are several ways dictionaries and lists can be nested with each other, including an example of how to use them
More layers of nesting are used in a similar way, just use them all the time, and loop them several times as you iterate!
Are you a loser today?!