TAG:.net
集合
((I)).集合類型 1.一般集合 I.Array a.Array中的秩是Array中的維數(shù).一個Array可以有一個或多個秩. Array具有固定的容量.如果有可變?nèi)萘?則用Array.CreateInstance,其可以不從零開始存儲. II.ArrayList集合類型 a.是數(shù)組的復(fù)雜版本.Array是數(shù)組是固定的,而ArrayList類是根據(jù)需要自動擴展的.如果更改了Array.Capacity屬性的值,則自動 進行內(nèi)存重新分配和元素復(fù)制. b.ArrayList提供添加/或移除某一范圍元素的方法.在Array中,只能一次獲取或設(shè)置一個元素的值. c.使用 Synchronized方法可以很容易地創(chuàng)建ArrayList的同步版本.而Array將一直保持它,直到用戶實現(xiàn)同步為止. d.ArrayList提供將只讀和固定大小包裝返回到集合的方法.而Array不提供. e.Array提供ArrayList所不具有的某些靈活性. I.可以設(shè)置Array的下限,但ArrayList的下限始終為零. II.Array可以具有多個維度,而ArrayList始終是唯一的. III.Array是特定類型(不是Object),比ArrayList性能好.ArrayList在存儲和檢索時經(jīng)常發(fā)生拆箱和裝箱操作現(xiàn)象. III.哈希表集合 a.Hashtable類基于IDictionary接口,因此該集合中的每一元素是鍵和值對. b.Object.GetHashCode方法為其自身生成哈希代碼.還可以通過使用Hashtable構(gòu)造函數(shù),為所有元素指定一個哈希函數(shù). IV.SortedList集合類型 a.SortedList類類似于Hashtable和ArrayList間的混合. b.SortedList的每一元素都是鍵對值,提供只返回鍵列表或只返回值列表的方法. c.如果想要一個保留鍵和值的集合,并且還需要索引的靈活性,則使用SortList. V.隊列集合類型 a.如果需要以信息在集合中存儲的相同順序來訪問這些信息,請使用Queue. b.Enqueue將一個元素添加到Queue的隊尾. Dequeue從Queue處移除最舊的元素. Peek從Queue的開始處返回最舊的元素,但不將從Queue中移除. VI.堆棧集合類型 a.如果需要以信息在集合中存儲的相反順序來訪問這些信息,請使用Queue. b.Push在Stack的頂部插入一個元素. Pop在Stack的頂部移除一個元素. Peek返回處于Stack頂部的元素,但不將其從棧頂上移除. 2.位集合 I.BitArray a.BitArray是一個集合類,容量與計數(shù)相同.通過增加Length屬性來將元素添加到BitArray中;通過降低Length屬性將元素刪除. b.獨特方法,如 And/Or/Xor/Not/SetAll. c.位于 System.Collections中. II.BitVector32 a.速度快,精確存儲32位,并且同時存儲標志位和小整數(shù). b.位于 System.Collections.Specialized中. 3.專用集合 I.NameValueCollection a.基于NameObjectCollectionBase,但NameValueCollection可以接受每個鍵多個值,而 NameObjectCollectionBase接受每個鍵,但只有一個值. ((2)).選擇用哪種集合 *** Queue或Stack:需要一個序列列表,其中的元素在檢索后放棄.否則,用其它集合. *** Queue或Stack:按一定的順序訪問這些元素(先進先出,后進先出),如果隨機,用其它集合. *** 是否通過索引訪問每一個元素? * ArrayList和StringCollection 提供通過元素的從零開始的*索引*對其元素進行訪問. * (Hashtable) (SortedList) (ListDictionary) (StringDictionary) 提供通過元素的*鍵*對其元素進行訪問 * (NameObjectCollectionBase) 和 (NameValueCollection) 提供或者通過元素的從零開始的*索引*或者通過元素的*鍵*對其元素進行訪問. *** 每一元素將包含一個值/一個值和一個鍵的組合還是一個鍵和多個值的組合? * 一個值: 使用基于 IList 的任何集合 * 一個鍵和一個值: 使用基于 IDictionary 的任何集合. * 一個鍵和多個值: 使用 System.Collections.Specialized 命名空間中的 NameValueCollection 類. *** 是否需要用與元素方式不同的方式對元素排序? * Hashtable 通過鍵的哈希代碼對元素進行排序. * SortedList 基于 IComparer 實現(xiàn),通過鍵對元素進行排序. * ArrayList 提供 Sort方法該方法將 IComparer 實現(xiàn)作為一個參數(shù)采用. *** 是否需要信息的快速搜索和檢索? * 對于小集合(十項或更少),ListDictionary 快于 Hashtable. *** 是否需要只接受字符串的集合? * StringCollection (基于 IList) 和 StringDictionary (基于 IDictionary) 位于 System.Collections.Specialized 命名空間中.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
由IComparer派生的自定義比較器
(一). 說明
1.繼承IComparer接口,可以自定義比較器 2.由于Array.Sort()方法接受IComparer參數(shù),進行自定義排序規(guī)則. 示例中也將IComparer作為Sort方法的參數(shù),將Icomparer應(yīng)用于Array.Sort()方法
(二).示例代碼
using System; using System.Collections;
namespace 比較器IComparer { /// /// Class1 的摘要說明。 /// public class Person { public int ID; public int Age; public Person() { ID=0; Age=0; } public Person(int id,int age) { ID=id; Age=age; } public void Show() { Console.WriteLine("年齡={0},代號={1}",Age,ID); } public static void ShowPersons(Person []persons) { foreach(Person person in persons) Console.WriteLine("年齡={0},代號={1}",person.Age,person.ID); } } public class PersonComparer:System.Collections.IComparer { int System.Collections.IComparer.Compare(object x,object y) { if(x==null||y==null) throw new ArgumentException("參數(shù)不能為空"); Person temp=new Person(); if(!x.GetType().Equals(temp.GetType())||!y.GetType().Equals(temp.GetType())) throw new ArgumentException("類型不一致"); temp=null; Person personX=(Person)x; Person personY=(Person)y; if(personX.ID>personY.ID) return 1; if(personX.ID
return -1; if(personX.Age>personY.Age) return 1; if(personX.Age
return -1; return 0; } } class Class1 { /// /// 應(yīng)用程序的主入口點。 /// [STAThread] static void Main(string[] args) { // // TODO: 在此處添加代碼以啟動應(yīng)用程序 // Random rand=new Random(); Person[] persons=new Person[6]; Console.WriteLine("隨機產(chǎn)生的Person數(shù)組為:"); for(int i=0;i
{ persons[i]=new Person(); persons[i].ID=rand.Next()%10; persons[i].Age=rand.Next()%50; persons[i].Show(); } PersonComparer personComparer=new PersonComparer(); //System.Collections.IComparer personComparer=new IComparer; Array.Sort(persons,personComparer); Console.WriteLine("排序后的結(jié)果:"); Person.ShowPersons(persons); Person personToBeFind=new Person(); Console.WriteLine("輸入ID"); personToBeFind.ID=int.Parse(Console.ReadLine()); Console.WriteLine("輸入Age"); personToBeFind.Age=int .Parse(Console.ReadLine()); int index=Array.BinarySearch(persons,personToBeFind,personComparer); if(index>=0) Console.WriteLine("待查找的元素是數(shù)組的第{0}個元素",index+1); else Console.WriteLine("對不起,沒有所找的元素"); Console.ReadLine(); } } }
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
用SortedList實現(xiàn)排序
using System; using System.Collections;
namespace 集合的比較和排序 { public class Efficience:IComparable { private int workHour; private int outPut; int IComparable.CompareTo(Object obj) { if(obj==null) throw new ArgumentException("比較對象不能為空"); if(!obj.GetType().Equals(this.GetType())) throw new ArgumentException("比較的兩者類型不同"); Efficience objEffic=(Efficience)obj; if(this.Effic>objEffic.Effic) return 1; if(this.Effic return -1; return 0; } public int WorkHour { set { if(value<0) throw new ArgumentException("工作時間不能為{0}或負數(shù)"); workHour=value; } } public int OutPut { set { if(value<0) throw new ArgumentException("工作產(chǎn)出不能為負數(shù)"); outPut=value; } } public float Effic { get { return (float)outPut/(float)workHour; } } } class Class1 { [STAThread] static void Main(string[] args) { Random rand=new Random(); Efficience[] effics=new Efficience[5]; string[] persons={"Xiaohua","Diana","YanYan","ZhuLin","LiXin"}; Console.WriteLine("生成的 Effics 數(shù)組"); //Console.WriteLine("effics.GetLowerBound(0)={0},effics.GetUpperBound(0)={1}",effics.GetLowerBound(0),effics.GetUpperBound(0)); for(int i=effics.GetLowerBound(0);i<=effics.GetUpperBound(0);i++) { effics[i]=new Efficience(); effics[i].WorkHour=rand.Next()%24; effics[i].OutPut=rand.Next()%1000; Console.WriteLine("Person={0},Effic={1}",persons[i],effics[i].Effic); } SortedList sortedList=new SortedList(); for(int i=effics.GetLowerBound(0);i<=effics.GetUpperBound(0);i++) { sortedList.Add(effics[i],persons[i]); } Console.WriteLine("從 sortedList 中讀取內(nèi)容"); foreach(Efficience effic in sortedList.GetKeyList()) { Console.WriteLine("Person={0},Effic={1}",sortedList[effic],effic.Effic); } Console.Read(); } } }
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
集合的拷貝
using System; using System.Collections; using System.Collections.Specialized;
namespace 集合的拷貝 { /// /// Class1 的摘要說明。 /// class Class1 { [STAThread] static void Main(string[] args) { NameValueCollection namedVColl=new NameValueCollection(); namedVColl.Add("曉華","13510532686"); namedVColl.Add("曉華","62658888"); namedVColl.Add("小楊","1361030486"); namedVColl.Add("小楊","62293218"); foreach(string key in namedVColl.Keys) Console.WriteLine("姓名={0},電話={1}",key,namedVColl[key]); Console.WriteLine("拷貝后獲得的命名值集合"); string[] arr=new string[namedVColl.Count]; namedVColl.CopyTo(arr,0); for(int i=arr.GetLowerBound(0);i { Console.WriteLine("姓名={0},電話={1}",namedVColl.AllKeys[i],arr[i]); } Hashtable hhtable=new Hashtable(); hhtable["曉華"]="北京"; hhtable["小楊"]="南京"; Console.WriteLine("克隆前哈希表"); foreach(object key in hhtable.Keys) Console.WriteLine("姓名={0},地址={1}",key,hhtable[key]); Hashtable hhCloned=(Hashtable)hhtable.Clone(); hhtable["曉華"]="上海"; Console.WriteLine("克隆修改初始化哈希表"); foreach(object key in hhtable.Keys) Console.WriteLine("姓名={0},地址={1}",key,hhtable[key]); Console.WriteLine("修改后初始化哈希表后的克隆哈薩克希表"); foreach(object key in hhCloned.Keys) { Console.WriteLine("姓名={0},地址={1}",key,hhCloned[key]); } Console.Read(); } } }
|