集合与列表

 一、回顾:
对象数组要能够排序,需要对象实现的接口是什么?
实现IComparable接口的CompareTo方法。可以使用Array类的Sort方法实现排序。

二、System.Collections命名空间
1.散列表(Hashtable)
散列表:表示键/值对的集合,这些键/值对根据键的哈希代码进行组织。
    Hashtable实现了IDictionary接口,因此在Hashtable中存储的是DictionaryEntry类型。
举例    Person zhang = new Person("张","三");
        Person li = new Person("李", "四");
        Person wang = new Person("王", "五");
        Hashtable hash = new Hashtable();
        hash.Add("zhang", zhang);
        hash.Add("li", li);
        hash.Add("wang", wang);
        foreach (DictionaryEntry p in hash)
        {
            Person pp = p.Value as Person;
            Response.Write(pp.FirstName + pp.LastName + "<br/>");
        }
        Response.Write("----------------<br/>");
        foreach (Person p in hash.Values)  //Values为Person的集合
        {
            Response.Write(p.FirstName + p.LastName + "<br/>");
        }
2.SortedList:自己排好序了
SortedList与Hashtable类似,也是一个关键字/值对的集合,但它按照其关键字来排序,其值可以通过数字索引来处理,与数组一样。
        SortedList list = new SortedList();
        list.Add("d", zhang);
        list.Add("b", li);
        list.Add("c", wang);
        int index = list.IndexOfKey("d");
        Response.Write(index + "<br/>");
        index = list.IndexOfValue(zhang);
        Response.Write(index + "<br/>");
        Person p = (Person)list.GetByIndex(index);
        Person pp = (Person)list["d"];
3.Queue
Queue表示对象的先进先出集合。存储消息方面非常有用,以便于进行顺序处理。
        Queue qu = new Queue();
        qu.Enqueue("hello ");
        qu.Enqueue("world ");
        qu.Enqueue("!");
        int nums = qu.Count;
        for (int i = 0; i < nums; i++)
            Response.Write(qu.Dequeue().ToString());
        Response.Write("------------------<br/>");

        foreach (string str in qu)
        {
            Response.Write(str);
        }
4.Stack
Stack和Queue相反,表示对象的简单的后进先出非泛型集合。
        Stack sk = new Stack();
        sk.Push("hello ");
        sk.Push("world ");
        sk.Push("!");
        int nums = sk.Count;
        for (int i = 0; i < nums; i++) //典型的后进先出结构
        {
            Response.Write(sk.Pop().ToString());
        }
5.强类型列表
        List<Person> list = new List<Person>();
        List<int> listi = new List<int>();
        List<string> lists = new List<string>();

        #region
        Person zhang = new Person("张", "三");
        Person li = new Person("李", "四");
        Person wang = new Person("王", "五");

        //ArrayList list = new ArrayList();
        //list.Add(zhang);
        //list.Add(li);
        //list.Add(wang);

        //强类型列表,personlist中只能存储Person对象,使用时可以避免强制转换时出错
        //2.0版本以后引入泛型概念,使用泛型列表可以解决这些问题
        PersonList list = new PersonList();
        list.Add(zhang);
        list.Add(li);
        list.Add(wang);

        for (int i = 0; i < list.Count; i++)
        {
            Response.Write(list[i].FirstName + list[i].LastName + "<br/>");
        }
        #endregion
        public class PersonList:IEnumerable
        {
           private ArrayList people;

           public PersonList()
           {
              people = new ArrayList();
           }
           public void Add(Person p)
           {
              people.Add(p);
           }
           public int Count
           {
              get { return this.people.Count; }
           }
           public Person this[int index]
           {
              get{return (Person)people[index];}
              set{people[index] = value;}
         }    
       
      
       
           
       
   

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值