C#集合類用法實(shí)例代碼詳解
下面介紹C#的集合類
1ArrayList
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace 動(dòng)態(tài)數(shù)組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("彈出最后一項(xiàng){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出隊(duì)");
foreach (int i in q1)
{
Console.WriteLine(i);
}
int num=(int)q2.Peek();
Console.WriteLine("取q2開(kāi)始處{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();
}
}
}
總結(jié)
以上所述是小編給大家介紹的C#集合類用法實(shí)例代碼詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)我們網(wǎng)站的支持!
上一篇:利用C#實(shí)現(xiàn)最基本的小說(shuō)爬蟲示例代碼
欄 目:C#教程
下一篇:C#帶你玩掃雷(附源碼)
本文標(biāo)題:C#集合類用法實(shí)例代碼詳解
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5445.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)實(shí)體類與字符串互相轉(zhuǎn)換的方法
- 01-10C#通過(guò)Semaphore類控制線程隊(duì)列的方法
- 01-10C#3.0使用EventLog類寫Windows事件日志的方法
- 01-10C#操作ftp類完整實(shí)例
- 01-10C#線程隊(duì)列用法實(shí)例分析
- 01-10winform簡(jiǎn)單緩存類實(shí)例
- 01-10C#實(shí)現(xiàn)控制攝像頭的類
- 01-10C#中Socket通信用法實(shí)例詳解
- 01-10WPF實(shí)現(xiàn)類似360安全衛(wèi)士界面的程序源碼分享
- 01-10C#中的事務(wù)用法實(shí)例分析


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-11Mac OSX 打開(kāi)原生自帶讀寫NTFS功能(圖文
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置


