C#语言集合

switch 用法
int x = int.Parse(Console.ReadLine());
switch(x){
case 1:
Console.WriteLine("这是1");
break;
case 2:
Console.WriteLine("这是2");
break;
case 3:
Console.WriteLine("这是3");
break;
}


冒泡排序
int[] a = new int[] { 1, 5, 6, 3, 4 };
for (int i = 0; i < a.Length;i++ ) {
for (int t = i+1; t < a.Length;t++ ) { 重点
if(a[i]>a[t]){
int s = a[i];
a[i] = a[t];
a[t] = s;
}
}
}
for (int i = 0; i< a.Length;i++ )
{
Console.WriteLine(a[i]);
}

普通集合
ArrayList p = new ArrayList();
p.Add("123");
p.Add(456);
p.Add(true);
p.Add("字");
for (int i = 0; i < p.Count; i++)
{
Console.WriteLine(p[i].ToString() + " " + p[i].GetType().ToString());

}

foreach (var x in p)
{
Console.WriteLine(x + " 空格 " + x.GetType());
}

x.ToString() + " " + x.GetType().ToString()

泛型集合
List<string> str_list = new List<string>();
str_list.Add("a");
str_list.Add("b");
str_list.Add("c");
str_list.Add("d");

foreach (string x in str_list) {
Console.WriteLine(x);
}


哈希表
Hashtable ht = new Hashtable();
ht.Add("1","a");
ht.Add("2","b");
ht.Add("3",false);
ht.Add("x",3.14);
Console.WriteLine(ht[2]);
foreach(var x in ht){
Console.WriteLine(x);
}

字典型
Dictionary<string, int> m = new Dictionary<string, int>();
m.Add("a", 3);
m.Add("b", 4);
m.Add("c", 5);
m.Add("d", 6);


foreach (var x in m.Values)
{
Console.WriteLine(x);
}

m.values 打印出"3456" m.keys 打印出"abcd"


队列
Queue que = new Queue();
que.Enqueue("张三");
que.Enqueue("李四");
que.Enqueue("王五");
que.Enqueue("赵六");

Console.WriteLine("现在的长度是"+que.Count);

Console.WriteLine(que.Dequeue());

Console.WriteLine("现在的长度是" + que.Count);

输出张三,现在的长度是3

堆栈
Stack st = new Stack();
st.Push("a");
st.Push("b");
st.Push("c");
st.Push("d");


Console.WriteLine(st.Count);


Console.WriteLine(st.Pop());
Console.WriteLine(st.Count);

输出d ,长度是3

原文地址:https://www.cnblogs.com/yunpeng521/p/7008720.html