ASP.net 中Queue,Stack,Hashtable,Sortlist一些簡單用法

c979170768發表於2011-11-07
 

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

相關文章