几种常用的集合类用法

时间:2021-11-14 17:01:45

//列表比较基础就不写了 其他的不太常用

// Dictionary 字典

Dictionary<int, EQInfo> eqs = new Dictionary<int, EQInfo>();
EQInfo tempInfo = new EQInfo();
for (int i = 1; i <= 11; i++)
{
tempInfo.ID = 1212 * i;
tempInfo.buyMoney = 220 * i;
tempInfo.sealMoney = 110 * i;
eqs.Add(tempInfo.ID, tempInfo);
}

EQInfo tempEq;
if (eqs.TryGetValue(1212, out tempEq))
{
Console.WriteLine(tempEq.buyMoney + " " + tempEq.sealMoney);
}var dicSort = from objDic in eqs orderby objDic.Value.buyMoney descending select objDic;

foreach (KeyValuePair<int, EQInfo> item in dicSort)
{
Console.WriteLine(item.Value.buyMoney);
}

 

//字典降序排序方法
public static void DictonarySort(Dictionary<string, int> dic)
{
var dicSort = from objDic in dic orderby objDic.Value descending select objDic;

foreach (KeyValuePair<string, int> kvp in dicSort)
Console.WriteLine(kvp.Key + ":" + kvp.Value + "<br />");
}

//字典升序排序方法
public static void DictonarySort(Dictionary<string, int> dic)
{
var dicSort = from objDic in dic orderby objDic.Value ascending select objDic;

foreach (KeyValuePair<string, int> kvp in dicSort)
Console.WriteLine(kvp.Key + ":" + kvp.Value + "<br />");
}



// Stack  堆栈    后进先出

Stack<int> sta = new Stack<int>();
//int staCou = sta.Count;
for (int i = 0; i < 10; i++)
{
sta.Push(i);//入栈
}
sta.Pop();//出栈
foreach (var item in sta)
{
Console.WriteLine(item);
}

// Queue  队列    先进先出

Queue<int> que = new Queue<int>();
for (int i = 0; i < 10; i++)
{
que.Enqueue(i);
}


Console.WriteLine(que.Dequeue() + "----------");
foreach (var item in que)
{
Console.WriteLine(item);