這兩種數據結構用起來確實方便,前日看一段程序,卻發現一人寫的程序中用到了隊列裡邊放了字符串
但是並不是用的這個System.Collections.Queue(或Stack、Hashtable)而是自己寫的隊列,
既然用了.Net框架也就是系統已經存在這類,自己寫了幾百行又不是一二十行,那就應該使用已經寫好的類。
可能是他不會用吧,今放於此供初學者學習,例子如下DOS程序:
using System;
using System.Collections;
class hello
...{
static void Main()
...{
CreatClass myClsMsg=new CreatClass();
Console.WriteLine(myClsMsg.accHelloMessage);//讀屬性
myClsMsg.accHelloMessage="這是測試.";
myClsMsg.ShowMessage();
Console.ReadLine();//暫停
}
}
public class CreatClass
...{string helloMessage="類原始信息";
public void ShowMessage()
...{//堆棧和隊列都實現了ICollection,IEnumerable,ICloneable這三個接口
Stack mStack = new Stack(3);//堆棧Peek()Pop()Push()Count
mStack.Push(1);
mStack.Push(2);
mStack.Push(3);
Console.WriteLine(helloMessage + "先用Peek再用Pop");
Console.WriteLine(mStack.Peek());
Console.WriteLine(mStack.Peek());
Console.Write(mStack.Pop()); Console.WriteLine(" 堆棧內的對象個數:{0}", mStack.Count);
Console.Write(mStack.Pop()); Console.WriteLine(" 堆棧內的對象個數:{0}", mStack.Count);
Console.Write(mStack.Pop()); Console.WriteLine(" 堆棧內的對象個數:{0}", mStack.Count);
Queue mQueue = new Queue();//對象默認的大小是32而等比級數因子是2.0當加入的元素數目超過默認容量,則集合對象增加的容量為目前容量乘以此級數
//public queue(int intcapacity,float fgrowfact)
//方法Enqueue()加入對象,Dequeue()取出元素並從集合中刪除Peek()只取出
mQueue.Enqueue(1);
mQueue.Enqueue(2);
mQueue.Enqueue(3);
Console.Write(mQueue.Dequeue()); Console.WriteLine(" 隊列內的對象個數:{0}", mQueue.Count);
Console.Write(mQueue.Dequeue()); Console.WriteLine(" 隊列內的對象個數:{0}", mQueue.Count);
Console.Write(mQueue.Dequeue()); Console.WriteLine(" 隊列內的對象個數:{0}", mQueue.Count);
//------Hashtable--------(SortedList類)與其類似--------
SortedList mySortL = new SortedList();//(int 容量)
mySortL.Add("Key=1", "value=1");
mySortL.Add("Key=2", "value=2");
Console.WriteLine("SortedList {0} {1}", mySortL.GetKey(0), mySortL.GetByIndex(0).ToString());
Console.WriteLine("SortedList {0} {1}", mySortL.GetKey(1), mySortL.GetByIndex(1).ToString());
//-----------------------------------------------------
Hashtable myhashtable = new Hashtable(3, 1);
myhashtable.Add("Key=1", "value=1");
myhashtable.Add("Key=2", "value=2");
myhashtable.Add("Key=3",&n
bsp;"value=3");
ICollection myKeys = myhashtable.Keys, myValues = myhashtable.Values;
foreach (string sk in myKeys) Console.WriteLine("HashK {0}", sk);
foreach (string sk in myValues) Console.WriteLine("HashV {0}", sk);
Console.WriteLine("----------------------------------");
//---------IDictionaryEnumerator字典枚舉---------------
IDictionaryEnumerator myIDE = myhashtable.GetEnumerator();
while (myIDE.MoveNext())
...{
Console.WriteLine("字典枚舉 {0} {1}", myIDE.Key, myIDE.Value);
}
do
...{
Console.Write("輸入Key值1,2,3 or b exit:");
string kv = Console.ReadLine();
if (kv == "b") break;
Console.WriteLine("HashV when Key=" + kv + " then {0}", myhashtable["Key=" + kv]);
} while (true);
}
public string accHelloMessage
...{ get...{return helloMessage;}
set...{helloMessage=value;}
}
}
using System;
using System.Text.RegularExpressions;
class UsingRe
...{
static void Main()
...{
string strTest="abc bb abc aaeebbcc aalll ee ff abc aa rr JSh abc ee rr";
string matching;
Console.Write("原是字符串:{0} 請輸入文字對比樣式:",strTest);
matching = Console.ReadLine();
Regex myregex = new Regex(matching);
MatchCollection myMatchc = new myregex.Matches(strTest);
foreach (Match myMatch in myMatchc)
...{
Console.WriteLine("{0}在第{1}個字符處被發現!!",myMatch,myMatch.Index);
}
}
}