随笔


  #region 数组
            
int[] lengths = new int[2] { 35 };
            
int[] starts = new int[2] { 23 };

            
//简历数组
            Array myArray = Array.CreateInstance(typeof(string), lengths, starts);
            
//初始化数组
            for (int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++)
            {
                
for (int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++)
                {

                }
            }
            
#endregion

            
#region HashTable操作

            
//像Hashtable里面添加值
            Hashtable ht = new Hashtable();
            ht.Add(
"BMW""宝马");
            ht.Add(
"FRERARI""法拉利");
            ht.Add(
"FORD""福特");
            
//删除福特元素
            ht.Remove("FORD");           

            
//修改Hashtable里面的值
            Hashtable ht1 = new Hashtable();
            ht1.Add(
"BMW""宝马");
            ht1.Add(
"FERRARI""法拉利");
            ht1.Add(
"FORD""福特");
            ht1[
"BMW"= "别摸我";

            Hashtable ht2 
= new Hashtable();
            ht2.Add(
"BMW","宝马");
            ht2.Add(
"FERRARI","法拉利");
            ht2.Add(
"FORD","福特");

            
string s = ht2["BMW"as string;
            
//通过DictionaryEntry dic in ht
            foreach (DictionaryEntry dic in ht2)
            {
                
string key = dic.Key as string;
                
string value = dic.Value as string;
            }

            
//通过键集合来遍历Hashtable
            foreach (string skey in ht2.Keys)
            {
                
string value = ht[skey] as string;
            }



            Hashtable ht 
= new Hashtable();
            ht.Add(
"BENZ""奔驰");
            ht.Add(
"BMW""宝马");

            
//假如集合中没有键"Cadillac"则添加元素
            if (!ht.Contains("CADILLAC"))
                ht.Add(
"CADILLAC""凯迪拉克");
            
//假如集合中没有值"丰田"则添加元素
            if (!ht.ContainsValue("丰田"))
                ht.Add(
"TOYOTA""丰田");
            
//显示元素个数
            Console.WriteLine("目前元素个数:{0}", ht.Count);
            Console.WriteLine(
"目前元素有:");

            
//遍历元素
            foreach (DictionaryEntry de in ht)
            {
                Console.WriteLine(
"KEY=\"{0}\"tValue=\"{1}\"", de.Key, de.Value);
            }
            ht.Remove(
"BENZ");
            
//利用ArrayList对Hashtable键值进行排序
            ArrayList aKeys = new ArrayList(ht.Keys);
            aKeys.Sort();
            
//遍历元素
            Console.WriteLine("排序后元素:");
            
foreach (string key in aKeys)
                Console.WriteLine(
"Key=\"{0}\"\tValue=\"{1}\"", key, ht[key].ToString());

            
//删除所有元素
            ht.Clear();
            
#endregion

            
#region IEnumerator接口极其使用
            ArrayList al 
= new ArrayList();
            al.Add(
"红酒");
            al.Add(
"白酒");
            al.Add(
"咖啡");
            IEnumerator itor 
= al.GetEnumerator();
            
while (itor.MoveNext())
            {
                
//Current返回Object对象,所以要进行类型转换
                string val = itor.Current as string;
                
//输出获得的结果
                Console.WriteLine(val);
            }

            
string[] drinks = { "红酒""白酒""咖啡" };
            IEnumerator ie 
= drinks.GetEnumerator();
            
while (ie.MoveNext())
            {
                
string val = ie.Current as string;
                Console.WriteLine(val);
            }


            Hashtable cars 
= new Hashtable();
            cars.Add(
"ROLLS-ROYCE""劳斯莱斯");
            cars.Add(
"LINCOLN""林肯");
            IEnumerator itor 
= cars.GetEnumerator();
            
while (itor.MoveNext())
            {
                
//遍历和输出
                DictionaryEntry val = (DictionaryEntry)itor.Current;
                Console.WriteLine(
"{0},{1}",val.Key,val.Value);
            }


            Hashtable cars 
= new Hashtable();
            cars.Add(
"ROLLS-ROYCE""劳斯莱斯");
            cars.Add(
"LINCOLN""林肯");
            IDictionaryEnumerator itor 
= cars.GetEnumerator();
            
while (itor.MoveNext()) 
            {
                
//遍历和输出
                Console.WriteLine("{0},{1}", itor.Key, itor.Value);
            }



            
#endregion 

            
#region Stack类似于动态数组ArrayList
            Stack myStack 
= new Stack();
            myStack.Push(
"");
            myStack.Push(
"");
            myStack.Push(
"");
            myStack.Push(
"冬瓜");
            myStack.Push(
"thanks");
            Console.WriteLine(
"{0}", myStack.Pop());

            
foreach (object obj in myStack)
            {
                Console.WriteLine(
"{0}", obj);
            }
            
#endregion 

            
#region Queue队列数据结构就是虚拟现实中的排队。队列的操作是先进先出
            Queue myQueue 
= new Queue();
            myQueue.Enqueue(
"");
            myQueue.Enqueue(
"");
            myQueue.Enqueue(
"吃东莞");
            
//出对第一个元素
            Console.WriteLine("{0}", myQueue.Dequeue());
            
//进队第一个元素
            myQueue.Enqueue("西瓜");
            
//显示队列中的所有元素
            foreach (object obj in myQueue)
                Console.WriteLine(
"{0}", obj);
            
#endregion

            
#region SortedList集合是哈希表和数组的杂交品种
            
#endregion 

            
#region  StringCollection和StringDictionary更方便的操作字符串集合
            StringDictionary ht 
= new StringDictionary();
            ht.Add(
"BMW""宝马");
            ht.Add(
"FERRARI""法拉利");
            ht.Add(
"FORD""福特");
            
//找到宝马
            string s = ht["BMW"];
            Console.WriteLine(
"BMW是{0}", s);
            
//通过键集合来遍历StringDictionary
            foreach (string skey in ht.Keys)
            {
                
string value = ht[skey];
                Console.WriteLine(
"{0},{1}", skey, value);
            }
            
#endregion

            
#region 泛型哈希表-Dictionary
            Dictionary
<stringstring> oscar = new Dictionary<stringstring>();
            oscar.Add(
"哈莉贝瑞""《死囚之舞》");
            oscar.Add(
"朱迪单期""《携手人生》");
            oscar.Add(
"内科而基德曼""《红磨坊》");
            oscar.Add(
"坚尼弗康纳利""《美丽心灵》");
            oscar.Add(
"雷尼奇伟哥""《BJ单身日记》");
            
//删除元素
            oscar.Remove("坚尼弗康纳利");
            
//假如不存在元素则加入元素
            if (!oscar.ContainsKey("西西斯派克"))
                oscar.Add(
"西西斯派克""《不伦之恋》");

            
//显示容量和元素个数
            Console.WriteLine("74届奥斯卡最佳女主角及其电影:");
            
foreach (KeyValuePair<stringstring> kvp in oscar)
            {
                Console.WriteLine(
"姓名:{0},电影:{1}", kvp.Key, kvp.Value);
            }
            
//得到hash表中键的集合
            Dictionary<stringstring>.KeyCollection keyColl = oscar.Keys;
            
//遍历间的集合
            Console.WriteLine("最佳女主角:");
            
foreach (string s in keyColl)
            {
                Console.WriteLine(s);
            }
            
//使用TryGetValue方法获取指定键对应的值
            string slove = string.Empty;
            
if (oscar.TryGetValue("朱迪单期"out slove))
                Console.WriteLine(
"我最喜欢朱迪单期的电影{0}", slove);
            
else
                Console.WriteLine(
"没找到朱迪单期的电影");
            oscar.Clear();
            
#endregion

            
#region 泛型队列
            Queue
<string> oscarcn = new Queue<string>();
            oscarcn.Enqueue(
"中国式离婚");
            oscarcn.Enqueue(
"东京审判");
            oscarcn.Enqueue(
"满城尽带黄金甲");
            oscarcn.Enqueue(
"落叶归根");

            
foreach (string number in oscarcn)
            {
                Console.WriteLine(number);
            }

            
//出对
            Console.WriteLine("出对:{0}", oscarcn.Dequeue());
            Console.WriteLine(
"出对:{0}", oscarcn.Dequeue());
            
//如果不包含该元素则仅对
            if (!oscarcn.Contains("疯狂的石头"))
                oscarcn.Enqueue(
"疯狂的石头");
            Console.WriteLine(
"元素个数:{0}", oscarcn.Count);
            
//将队列copy到数组中
            string[] arrayC = new string[oscarcn.Count];
            oscarcn.CopyTo(arrayC, 
0);
            
//遍历数组
            Console.WriteLine("2006年中国最佳影片");
            
foreach (string number in arrayC)
            {
                Console.WriteLine(number);
            }
            oscarcn.Clear();
            
#endregion

            
#region 泛型栈
            Stack
<string> numbers = new Stack<string>();
            numbers.Push(
"\n内心深处都涌动着一段段伤心的故事");
            numbers.Push(
"\n在这个城市里面的每个人");
            numbers.Push(
"\n这是一座充满了伤心的城市");

            
//遍历栈集合元素
            foreach (string number in numbers)
            {
                Console.WriteLine(number);
            }

            
//进栈
            numbers.Push("\n伤城:");
            
string message = numbers.Pop() + numbers.Pop() + numbers.Pop() + numbers.Pop();
            Console.WriteLine(message);
            
//复制栈元素到数组
            string[] arrayC = new string[numbers.Count];
            numbers.CopyTo(arrayC, 
0);
            
//清空栈

            
#endregion 

            
#region 使用线程 我们知道委托可以代表一个方法,所以创建线程实例时只需要在Thread类的构造方法里面传入一个委托实例即可
            Thread 线程对象实例 
= new Thread(new ThreadStart(方法名));
            Thread.CurrentThread.Name 
= "主线程";
            Console.WriteLine(
"主线程启动");
            
//在主线程里面创建子线程
            Thread th = new Thread(new ThreadStart(ThreadProc));
            
//启动子线程
            th.Start();
            
//主线程开始数数
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(
"{0}:{1}", Thread.CurrentThread.Name, i);
                Thread.Sleep(
1);//让当前线程休眠毫秒
            }
            
#endregion

            
#region 线程的例外一种用法
            Thread.CurrentThread.Name 
= "主线程";
            Thread th 
= new Thread(new ThreadStart(DoWork));
            th.Name 
= "子线程";
            th.Start();
            DoWork();
            
#endregion 
posted @ 2009-06-29 14:35  paymob  阅读(219)  评论(0编辑  收藏  举报