ArrayList 類:使用大小可按需動態增加的數組。
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ArrayList al = new ArrayList();
al.Add(100);//單個添加
foreach (int number in new int[6] { 9, 3, 7, 2, 4, 8 })
{
al.Add(number);//集體添加方法一
}
int[] number2 = new int[2] { 11, 12 };
al.AddRange(number2);//集體添加方法二
al.Remove(3);//移除值為3的
al.RemoveAt(3);//移除第3個
ArrayList al2 = new ArrayList(al.GetRange(1, 3));//新ArrayList只取舊ArrayList一部份
Console.WriteLine("遍曆方法一:");
foreach (int i in al)//不要強制轉換
{
Console.WriteLine(i);//遍曆方法一
}
Console.WriteLine("遍曆方法二:");
for (int i = 0; i < al2.Count; i++)//數組是length
{
int number = (int)al2[i];//一定要強制轉換
Console.WriteLine(number);//遍曆方法二
}
}
}
}
C# 集合類(二):Queue
Posted on 2008-01-14 10:16 無鋒不起浪 閱讀(413) 評論(3) 編輯 收藏 所屬分類: C# 2.0
Queue:隊列,表示對象的先進先出集合。Enqueue方法入隊列,Dequeue方法出隊列。
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Queue qu = new Queue();
Queue qu2 = new Queue();
foreach (int i in new int[4] { 1, 2, 3, 4 })
{
qu.Enqueue(i);//入隊
qu2.Enqueue(i);
}
foreach (int i in qu)
{
Console.WriteLine(i);//遍曆
}
qu.Dequeue();//出隊
Console.WriteLine("Dequeue");
foreach (int i in qu)
{
Console.WriteLine(i);
}
qu2.Peek();//返回位於 Queue 開始處的對象但不將其移除。
Console.WriteLine("Peek");
foreach (int i in qu2)
{
Console.WriteLine(i);
}
}
}
C# 集合類(三):Stack
Stack:棧,表示對象的簡單的後進先出非泛型集合。Push方法入棧,Pop方法出棧。
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Stack sk = new Stack();
Stack sk2 = new Stack();
foreach (int i in new int[4] { 1, 2, 3, 4 })
{
sk.Push(i);//入棧
sk2.Push(i);
}
foreach (int i in sk)
{
Console.WriteLine(i);//遍曆
}
sk.Pop();//出棧
Console.WriteLine("Pop");
foreach (int i in sk)
{
Console.WriteLine(i);
}
sk2.Peek();//彈出最後一項不刪除
Console.WriteLine("Peek");
foreach (int i in sk2)
{
Console.WriteLine(i);
}
}
}
}
C# 集合類(五):SortedList
SortedList類:表示鍵/值對的集合,與雜湊表類似,區別在於SortedList中的Key數組排好序的。
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
public static void Main()
{
SortedList sl = new SortedList();
sl["c"] = 41;
sl["a"] = 42;
sl["d"] = 11;
sl["b"] = 13;
foreach (DictionaryEntry element in sl)
{
string s = (string)element.Key;
int i = (int)element.Value;
Console.WriteLine("{0},{1}", s, i);
}
}
}
}
C# 集合類(六):Dictionary 泛型集合
泛型最常見的用途是泛型集合,命名空間System.Collections.Generic 中包含了一些基於泛型的集合類,使用泛型集合類可
以提供更高的型別安全,還有更高的效能,避免了非泛型集合的重複的裝箱和拆箱。
很多非泛型集合類都有對應的泛型集合類,下面是常用的非泛型集合類以及對應的泛型集合類:
非泛型集合類 泛型集合類
ArrayList List<T>
HashTable DIctionary<T>
Queue Queue<T>
Stack Stack<T>
SortedList SortedList<T>
我們用的比較多的非泛型集合類主要有 ArrayList類 和 HashTable類。我們經常用HashTable 來儲存將要寫入到資料庫
或者返回的資訊,在這之間要不斷的進行類型的轉化,增加了系統裝箱和拆箱的負擔,如果我們操縱的資料類型相對確定的
化 用 Dictionary<TKey,TValue> 集合類來儲存資料就方便多了,例如我們需要在電子商務網站中儲存使用者的購物車資訊(
商品名,對應的商品個數)時,完全可以用 Dictionary<string, int> 來儲存購物車資訊,而不需要任何的類型轉化。
下面是簡單的例子,包括聲明,填充索引值對,移除索引值對,遍曆索引值對
Dictionary<string, string> myDic = new Dictionary<string, string>();
myDic.Add("aaa", "111");
myDic.Add("bbb", "222");
myDic.Add("ccc", "333");
myDic.Add("ddd", "444");
//如果添加已經存在的鍵,add方法會拋出異常
try
{
myDic.Add("ddd","ddd");
}
catch (ArgumentException ex)
{
Console.WriteLine("此鍵已經存在:" + ex.Message);
}
//解決add()異常的方法是用ContainsKey()方法來判斷鍵是否存在
if (!myDic.ContainsKey("ddd"))
{
myDic.Add("ddd", "ddd");
}
else
{
Console.WriteLine("此鍵已經存在:");
}
//而使用索引器來負值時,如果建已經存在,就會修改已有的鍵的索引值,而不會拋出異常
myDic ["ddd"]="ddd";
myDic["eee"] = "555";
//使用索引器來取值時,如果鍵不存在就會引發異常
try
{
Console.WriteLine("不存在的鍵\"fff\"的索引值為:" + myDic["fff"]);
}
catch (KeyNotFoundException ex)
{
Console.WriteLine("沒有找到鍵引發異常:" + ex.Message);
}
//解決上面的異常的方法是使用ContarnsKey() 來判斷時候存在鍵,如果經常要取健值得化最好用 TryGetValue方法來
擷取集合中的對應索引值
string value = "";
if (myDic.TryGetValue("fff", out value))
{
Console.WriteLine("不存在的鍵\"fff\"的索引值為:" + value );
}
else
{
Console.WriteLine("沒有找到對應鍵的索引值");
}
//下面用foreach 來遍曆索引值對
//泛型結構體 用來儲存健值對
foreach (KeyValuePair<string, string> kvp in myDic)
{
Console.WriteLine("key={0},value={1}", kvp.Key, kvp.Value);
}
//擷取值得集合
foreach (string s in myDic.Values)
{
Console.WriteLine("value={0}", s);
}
//擷取值得另一種方式
Dictionary<string, string>.ValueCollection values = myDic.Values;
foreach (string s in values)
{
Console.WriteLine("value={0}", s);
}