继上篇,先把这些容器的基本语法学起来
跟上一篇同样的图 :
Dictionary<int, string> dictionary = new Dictionary<int, string>();
dictionary.Add(2, "AA");
dictionary.Add(23, "BB");
dictionary.Add(5, "CC");
dictionary.Add(1, "DD");
foreach (var item in dictionary)
{
Console.WriteLine($"key = {item.Key} , value = {item.Value}");
}
SortedDictionary<int, string> sortedDictionary
= new SortedDictionary<int, string>();
sortedDictionary.Add(2, "AA");
sortedDictionary.Add(23, "BB");
sortedDictionary.Add(5, "CC");
sortedDictionary.Add(1, "DD");
foreach (var item in sortedDictionary)
{
Console.WriteLine($"key = {item.Key} , value = {item.Value}");
}
internal class Person
{
public int Age { get; set; }
public string Name { get; set; }
}
实作ICompare并定义排序条件
internal class PersonComparer : IComparer<Person>
{
public int Compare(Person p1, Person p2)
{
int result;
result = p1.Age.CompareTo(p2.Age);
if (result == 0)
result = p1.Name.CompareTo(p2.Name);
return result;
}
}
宣告时放入PersonComparer
SortedDictionary<Person, string> sortedDictionary2
= new SortedDictionary<Person, string>(new PersonComparer());
sortedDictionary2.Add(new Person { Name = "BB", Age = 6 }, "BB2");
sortedDictionary2.Add(new Person { Name = "AA", Age = 7 }, "AA2");
sortedDictionary2.Add(new Person { Name = "A", Age = 3 }, "A2");
sortedDictionary2.Add(new Person { Name = "CCC", Age = 30 }, "CCC2");
foreach (var item in sortedDictionary2)
{
Console.WriteLine($"key = {item.Key.Name}-{item.Key.Age}
, value = {item.Value}");
}
List<int> list = new List<int>();
list.Add(2);
list.Add(23);
list.Add(5);
list.Add(1);
foreach (var item in list)
{
Console.WriteLine($"value = {item}");
}
SortedList<int, string> sortList = new SortedList<int, string>();
sortList.Add(2, "AA");
sortList.Add(23, "BB");
sortList.Add(5, "CC");
sortList.Add(1, "DD");
foreach (var item in sortList)
{
Console.WriteLine($"key = {item.Key} , value = {item.Value}");
}
HashSet<int> hashSet = new HashSet<int>
{
2,23,5,1
};
foreach (var item in hashSet)
{
Console.WriteLine($"value = {item}");
}
SortedSet<int> sortedSet = new SortedSet<int>();
sortedSet.Add(2);
sortedSet.Add(23);
sortedSet.Add(5);
sortedSet.Add(1);
foreach (var item in sortedSet)
{
Console.WriteLine($"value = {item}");
}
Stack<int> stack = new Stack<int>();
stack.Push(9);
stack.Push(78);
stack.Push(66);
stack.Push(55);
Console.WriteLine($"Peek = {stack.Peek()}");
while (stack.Count() != 0)
{
Console.WriteLine($"value = {stack.Pop()}");
}
Queue<int> queue = new Queue<int>();
queue.Enqueue(9);
queue.Enqueue(78);
queue.Enqueue(66);
queue.Enqueue(55);
while (queue.Count() != 0)
{
Console.WriteLine($"value = {queue.Dequeue()}");
}
LinkedList<int> linkList = new LinkedList<int>();
linkList.AddFirst(1);
linkList.AddFirst(2);
linkList.AddFirst(3);
linkList.AddLast(7);
linkList.AddLast(8);
linkList.AddLast(9);
linkList.RemoveFirst();
linkList.RemoveLast();
foreach (var item in linkList)
{
Console.WriteLine($"value = {item}");
}
熟悉基本语法後再找适当的演算法放不同容器试一试
新手发文,若有错误的地方请不吝色的指正,谢谢。
<<: 微服务、容器化和无服务器(Microservices, Containerization, and Serverless)
>>: Day3 参加职训(机器学习与资料分析工程师培训班),记录学习内容(6/30-8/20)
Oracle row to column在10G版本仅能使用 CASE or DECODE,11G版...
MySQL Sakila Sample Database https://dev.mysql.com...
overseas education consultants in delhi Abroad edu...
针对主动情蒐 主动情蒐指主动跟目标进行互动,会透过工具枚举所需的资讯,本系列文会介绍以下几种工具。 ...
在测试这项范例之前我一直搞不懂在过去测试的时候我记得 test db 不会清除资料,後来查资料才发现...