ASP.net 中Queue,Stack,Hashtable,Sortlist一些简单用法

时间:2023-01-30 09:04:44
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


      

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Queue qu = new Queue();
        qu.Enqueue("hello");
        qu.Enqueue("word");
        qu.Enqueue("!");

        //foreach (string item in qu)
        //{
        //    Response.Write(item);
        //}
        int m = qu.Count;
        Response.Write("Queu先进先出:");
        for (int i = 0; i < m; i++)
        {
            Response.Write(qu.Dequeue());
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Stack st = new Stack();
        st.Push(" hello");
        st.Push(" World");
        st.Push(" !");
     //   Response.Write(st.Peek());
     //   st.Pop();
        Response.Write("Stack先进后出:");
        //foreach (string item in st)
        //{
        //    Response.Write(item);
        //}
        int mm = st.Count;
        for (int i = 0; i < mm; i++)
        {
            Response.Write(st.Pop());
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Person p = new Person("张", "三");
        Person li = new Person("李", "四");
        Person wang = new Person("王", "五");
        Hashtable table = new Hashtable();
        table.Add("z", p);
        table.Add("li", li);
        table.Add("wang", wang);
        Response.Write("Hashtable通过三种遍历方法进输出的</br>");
        foreach (DictionaryEntry item in table)
        {
            Person s = item.Value as Person;
            Response.Write(s.FirstName + s.LastName + "</br>");
        }

        Response.Write("以上通过遍历Hashtable转换其类型后输出</br>");
        foreach (Person s in table.Values)
        {
            Response.Write(s.FirstName + s.LastName + "</br>");
        }
        Response.Write("以上通过遍历Hashtable的Values转换其类型后输出</br>");
        foreach (string s in table.Keys)
        {
            Person p5 = table[s] as Person;
            Response.Write(p5.FirstName + p5.LastName + "</br>");
        }
        Response.Write("以上通过遍历Hashtable的Keys转换其类型后输出" + "</br>");

    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Person p = new Person("张", "三");
        Person li = new Person("李", "四");
        Person wang = new Person("王", "五");
        Hashtable table = new Hashtable();
        table.Add("z", p);
        table.Add("li", li);
        table.Add("wang", wang);

        ArrayList list = new ArrayList(table.Values);  //ArrayList list = new ArrayList(table.Values);
        foreach (Person item in list)
        {
            Response.Write(item.FirstName + item.LastName);
        }
        Response.Write("</br>以上是没有排序过的</br>");
        list.Sort();
        foreach (Person item in list)
        {
            Response.Write(item.FirstName + item.LastName);
        }

    
    }
    protected void Button5_Click(object sender, EventArgs e)
    {

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

        SortedList sortlist = new SortedList();
        sortlist.Add("z", li);
        sortlist.Add("a", p);
        sortlist.Add("b", wang);

        Response.Write("李四,张三,王五</br>");
        Response.Write("用sortlist之后自动按keys进行排序</br>");
        foreach (Person item in sortlist.Values)
        {
            Response.Write(item.FirstName + item.LastName);
        }


    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        Person p = new Person("张", "三");
        Person li = new Person("李", "四");
        Person wang = new Person("王", "五");

        SortedList sortlist = new SortedList();
        sortlist.Add("z", li);
        sortlist.Add("a", p);
        sortlist.Add("b", wang);

        int index = sortlist.IndexOfKey("z");  //得出z所有的索引
       // Response.Write(index);
        index = sortlist.IndexOfValue(li);   //返回指定值所有的索引
        //Response.Write(index);
        Person p2 = (Person)sortlist.GetByIndex(index);   //得到索引位置的值
        Response.Write(p2.FirstName + p2.LastName);
    }
}