C#集合类用法实例代码详解
程序员文章站
2023-12-13 20:17:10
下面介绍c#的集合类
1arraylist
using system;
using system.collections.generic;
using...
下面介绍c#的集合类
1arraylist
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.collections; namespace 动态数组arraylist { class program { static void main(string[] args) { arraylist a1 = new arraylist(); a1.add(100); foreach (int number in new int[6] { 9, 3, 7, 2, 4, 8 }) { a1.add(number); } int[] number2 = new int[2] { 11, 12 }; a1.addrange(number2); a1.remove(3); a1.removeat(3); arraylist al2 = new arraylist(a1.getrange(1,3)); console.writeline("遍历方法1:"); foreach (int i in a1) { console.writeline(i); } console.writeline("遍历方法2:"); for (int i = 0; i < al2.count; i++) { console.writeline(al2[i]); } console.readline(); } } }
2 stack
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.collections; namespace stack集合类 { class program { static void main(string[] args) { stack s1 = new stack(); stack s2 = new stack(); foreach (int i in new int[4] { 1, 2, 3, 4 }) { s1.push(i); s2.push(i); } foreach (int i in s1) { console.writeline(i); } s1.pop(); console.writeline("出栈"); foreach (int i in s1) { console.writeline(i); } int num=(int)s2.peek(); console.writeline("弹出最后一项{0}",num); foreach (int i in s2) { console.writeline(i); } console.readline(); } } }
3queue
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.collections; namespace queue集合类 { class program { static void main(string[] args) { queue q1 = new queue(); queue q2 = new queue(); foreach(int i in new int [4]{1,2,3,4}) { q1.enqueue(i); q2.enqueue(i); } foreach (int i in q1) { console.writeline(i); } q1.dequeue(); console.writeline("q1出队"); foreach (int i in q1) { console.writeline(i); } int num=(int)q2.peek(); console.writeline("取q2开始处{0}",num); foreach(int i in q2) { console.writeline(i); } console.readline(); } } }
4hashtable
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.collections; namespace hashtable集合类 { class program { static void main(string[] args) { hashtable h = new hashtable(); h.add("e","e"); h.add("b", "b"); h.add("c", "c"); h.add("a", "a"); foreach (dictionaryentry e in h) { console.write("{0},{1} ", e.key, e.value); } console.writeline(); string s = (string)h["c"]; console.writeline(s); if (h.contains("e")) { console.writeline("含有e"); } console.writeline(h["a"]); h.remove(h["a"]); h.clear(); foreach (dictionaryentry e in h) { console.write("{0},{1} ", e.key, e.value); } console.readline(); } } }
5sortedlist
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.collections; namespace sortedlist集合类 { class program { static void main(string[] args) { sortedlist s1 = new sortedlist(); s1["c"]=41; s1["a"]=42; s1["d"]=11; s1["b"]=13; foreach (dictionaryentry e in s1) { string s = (string)e.key; int i = (int)e.value; console.write("{0},{1} ",s,i); } console.readline(); } } }
总结
以上所述是小编给大家介绍的c#集合类用法实例代码详解,希望对大家有所帮助