小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

.Net中的Collection...

 ShangShujie 2008-01-22
TAG:.net

集合

  ((I)).集合類(lèi)型 
    1.一般集合
        I.Array
            a.Array中的秩是Array中的維數(shù).一個(gè)Array可以有一個(gè)或多個(gè)秩.
              Array具有固定的容量.如果有可變?nèi)萘?則用Array.CreateInstance,其可以不從零開(kāi)始存儲(chǔ).
        II.ArrayList集合類(lèi)型
            a.是數(shù)組的復(fù)雜版本.Array是數(shù)組是固定的,而ArrayList類(lèi)是根據(jù)需要自動(dòng)擴(kuò)展的.如果更改了Array.Capacity屬性的值,則自動(dòng)              進(jìn)行內(nèi)存重新分配和元素復(fù)制.
            b.ArrayList提供添加/或移除某一范圍元素的方法.在Array中,只能一次獲取或設(shè)置一個(gè)元素的值.
            c.使用 Synchronized方法可以很容易地創(chuàng)建ArrayList的同步版本.而Array將一直保持它,直到用戶(hù)實(shí)現(xiàn)同步為止.
            d.ArrayList提供將只讀和固定大小包裝返回到集合的方法.而Array不提供.
            e.Array提供ArrayList所不具有的某些靈活性.
                I.可以設(shè)置Array的下限,但ArrayList的下限始終為零.
                II.Array可以具有多個(gè)維度,而ArrayList始終是唯一的.
                III.Array是特定類(lèi)型(不是Object),比ArrayList性能好.ArrayList在存儲(chǔ)和檢索時(shí)經(jīng)常發(fā)生拆箱和裝箱操作現(xiàn)象.                  
        III.哈希表集合
            a.Hashtable類(lèi)基于IDictionary接口,因此該集合中的每一元素是鍵和值對(duì).
            b.Object.GetHashCode方法為其自身生成哈希代碼.還可以通過(guò)使用Hashtable構(gòu)造函數(shù),為所有元素指定一個(gè)哈希函數(shù).
        IV.SortedList集合類(lèi)型
            a.SortedList類(lèi)類(lèi)似于Hashtable和ArrayList間的混合.
            b.SortedList的每一元素都是鍵對(duì)值,提供只返回鍵列表或只返回值列表的方法.
            c.如果想要一個(gè)保留鍵和值的集合,并且還需要索引的靈活性,則使用SortList.
        V.隊(duì)列集合類(lèi)型
            a.如果需要以信息在集合中存儲(chǔ)的相同順序來(lái)訪問(wèn)這些信息,請(qǐng)使用Queue.
            b.Enqueue將一個(gè)元素添加到Queue的隊(duì)尾. Dequeue從Queue處移除最舊的元素. Peek從Queue的開(kāi)始處返回最舊的元素,但不將從Queue中移除.
        VI.堆棧集合類(lèi)型
            a.如果需要以信息在集合中存儲(chǔ)的相反順序來(lái)訪問(wèn)這些信息,請(qǐng)使用Queue.
            b.Push在Stack的頂部插入一個(gè)元素. Pop在Stack的頂部移除一個(gè)元素. Peek返回處于Stack頂部的元素,但不將其從棧頂上移除.
     2.位集合
        I.BitArray
           a.BitArray是一個(gè)集合類(lèi),容量與計(jì)數(shù)相同.通過(guò)增加Length屬性來(lái)將元素添加到BitArray中;通過(guò)降低Length屬性將元素刪除.
           b.獨(dú)特方法,如 And/Or/Xor/Not/SetAll.
           c.位于 System.Collections中.
        II.BitVector32
           a.速度快,精確存儲(chǔ)32位,并且同時(shí)存儲(chǔ)標(biāo)志位和小整數(shù).
           b.位于 System.Collections.Specialized中.
     3.專(zhuān)用集合
        I.NameValueCollection
           a.基于NameObjectCollectionBase,但NameValueCollection可以接受每個(gè)鍵多個(gè)值,而 NameObjectCollectionBase接受每個(gè)鍵,但只有一個(gè)值.  
   ((2)).選擇用哪種集合
     *** Queue或Stack:需要一個(gè)序列列表,其中的元素在檢索后放棄.否則,用其它集合.
     *** Queue或Stack:按一定的順序訪問(wèn)這些元素(先進(jìn)先出,后進(jìn)先出),如果隨機(jī),用其它集合.
     *** 是否通過(guò)索引訪問(wèn)每一個(gè)元素?
         * ArrayList和StringCollection 提供通過(guò)元素的從零開(kāi)始的*索引*對(duì)其元素進(jìn)行訪問(wèn).
         * (Hashtable) (SortedList) (ListDictionary) (StringDictionary) 提供通過(guò)元素的*鍵*對(duì)其元素進(jìn)行訪問(wèn)
         * (NameObjectCollectionBase) 和 (NameValueCollection) 提供或者通過(guò)元素的從零開(kāi)始的*索引*或者通過(guò)元素的*鍵*對(duì)其元素進(jìn)行訪問(wèn).
     *** 每一元素將包含一個(gè)值/一個(gè)值和一個(gè)鍵的組合還是一個(gè)鍵和多個(gè)值的組合?
         * 一個(gè)值: 使用基于 IList 的任何集合
         * 一個(gè)鍵和一個(gè)值: 使用基于 IDictionary 的任何集合.
         * 一個(gè)鍵和多個(gè)值: 使用 System.Collections.Specialized 命名空間中的 NameValueCollection 類(lèi).
     *** 是否需要用與元素方式不同的方式對(duì)元素排序?
         * Hashtable 通過(guò)鍵的哈希代碼對(duì)元素進(jìn)行排序.
         * SortedList 基于 IComparer 實(shí)現(xiàn),通過(guò)鍵對(duì)元素進(jìn)行排序.
         * ArrayList 提供 Sort方法該方法將 IComparer 實(shí)現(xiàn)作為一個(gè)參數(shù)采用.
     *** 是否需要信息的快速搜索和檢索?
         * 對(duì)于小集合(十項(xiàng)或更少),ListDictionary 快于 Hashtable.
     *** 是否需要只接受字符串的集合?
         * StringCollection (基于 IList) 和 StringDictionary (基于 IDictionary) 位于 System.Collections.Specialized 命名空間中.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

由IComparer派生的自定義比較器

(一). 說(shuō)明

1.繼承IComparer接口,可以自定義比較器
2.由于Array.Sort()方法接受IComparer參數(shù),進(jìn)行自定義排序規(guī)則.
   示例中也將IComparer作為Sort方法的參數(shù),將Icomparer應(yīng)用于Array.Sort()方法

(二).示例代碼

using System;
using System.Collections;

namespace 比較器IComparer
{
 ///


 /// Class1 的摘要說(shuō)明。
 ///

 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},代號(hào)={1}",Age,ID);
  }
  public static void  ShowPersons(Person []persons)
  {
   foreach(Person person in persons)               
    Console.WriteLine("年齡={0},代號(hào)={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("類(lèi)型不一致");
   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)用程序的主入口點(diǎn)。
  ///

  [STAThread]
  static void Main(string[] args)
  {
   //
   // TODO: 在此處添加代碼以啟動(dòng)應(yīng)用程序
   //
   Random rand=new Random();
   Person[] persons=new Person[6];
   Console.WriteLine("隨機(jī)產(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}個(gè)元素",index+1);
   else
    Console.WriteLine("對(duì)不起,沒(méi)有所找的元素");
   Console.ReadLine();
  }
 }
}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

用SortedList實(shí)現(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("比較對(duì)象不能為空");
   if(!obj.GetType().Equals(this.GetType()))
    throw new ArgumentException("比較的兩者類(lèi)型不同");
   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("工作時(shí)間不能為{0}或負(fù)數(shù)");
    workHour=value;
   }
  }
  public int OutPut
  {
   set
   {
    if(value<0)
     throw new ArgumentException("工作產(chǎn)出不能為負(fù)數(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 的摘要說(shuō)明。
 ///

 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();
  }
 }
}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多