asp.net2.0中的collections 和lists(Arrays)

來源:互聯網
上載者:User

主要想講一下關於collection在asp.net2.0的應用.

1.Arrays

Array是一種最簡單的集合對象(collection of object) .那麼先從一個person的類開始吧.

關於類的概念不多說了,開啟一個項目,建立一個App_Code的檔案夾(這裡面就是放置類檔案),強調一下名字最好必須.

點住App_Code檔案夾,添加一個person.cs的類檔案.

person.cs代碼如下

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for person
/// </summary>
public class person
{
    string FirstName;
    string LastName;
 public person(string first,string last)
 {
        FirstName = first;
        LastName = last;
        //
  // TODO: Add constructor logic her  //
 }

    public string FullName
    {
        get
        {
            return FirstName + " " + LastName;
        }
    }
}

然後建立一個showperson.aspx檔案,後置代碼showperson.aspx.cs

showperson.aspx.cs 內Page_load寫如下

    protected void Page_Load(object sender, EventArgs e)
    {
        person jimmy = new person("jimmy", "zeng");
        person stone = new person("stone", "wang");
        person helen = new person("Helen", "liu");

        person[] people = { jimmy, stone, helen };
        Response.Write("我們現在開始輸出姓名了<br/>");
        foreach(person p in people)
        {
            Response.Write(p.FullName+"<br/>");
        }
    }

意思就是person 產生三個姓名對象,分別是jimmy/stone/helen.然後組建了一個對象的array people.

最後使用foreach取得每個對象的FullName; 運行一下就可以看到結果了.

調整Arrays大小

在C#中無法調整Arrays的大小的.唯一的辦法就是,copy以前的array到一個容量大的array

person [ ] arypeople2 = new person [ 5]

Array.Copy (people,arypeople2,people.Length):

在Arrays中尋找對象
array是最collection最簡單的一種.我們一般都用索引值來快速定位(index).
當然我們在尋找object的時候,我們要弄清楚我們是要尋找一個確定屬性的object還是說要尋找同目標object擁有相同值的object.是兩個根本區別的概念哦.
ok
looking for an object in an array by reference
person [ ] people = { jimmy, stone, helen};
int indexofjimmy = Array.Indexof(people,jimmy);
Response.Write("jimmy is at "+indexofjimmy +" <br/>");
這個時候輸出就是
jimmy is at 0

再來一個例子
       person jimmy = new person("jimmy", "zeng");
        person stone = new person("stone", "wang");
        person helen = new person("Helen", "liu");    
        person[] people = { jimmy, stone, helen };
       person jimmy2 = new person("jimmy","zeng");
      int indexofjimmy2 = Array.Indexof(people,jimmy2);
Response.Write("jimmy2 is at " +indexofjimmy2+"<br/>");
啟動並執行幾個結果是: jimmy2 is at -1
當然了,表示是jimmy2根本不再people這個array裡面.當然我們的意圖倒不是尋找jimmy2這個object,而是想尋找同jimmy2一樣屬性的object.怎麼辦呢/?/?

說下兩個object的比較吧.
首先利用object.Equals我們override一下.
開啟APP_Code目錄下的person.cs 輸入下面的代碼重載一下就可以.

    public override bool Equals(object obj)
    {
        person other = obj as person;
        return (other.LastName == this.LastName && other.FirstName == this.FirstName);
    }
然後我們在showpeople.aspx.cs裡面作個比較
person jimmy2 = new person("jimmy", "zeng");
        if (jimmy2.Equals(helen) == true)
        {
            Response.Write("ok");
        }
        else
        {
            Response.Write("false");
        }
    }
輸出為false. ok成功比較對象.
匯入IComparable
為了便於為array排序,比較我們匯入IComparable
匯入辦法就是修改person.cs代碼如下看紅色的代碼處

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for person
/// </summary>
public class person:IComparable
{
    string FirstName;
    string LastName;
 public person(string first,string last)
 {
        FirstName = first;
        LastName = last;
        //
  // TODO: Add constructor logic her  //
 }

    public string FullName
    {
        get
        {
            return FirstName + " " + LastName;
        }
    }
}
ok 現在已經植入了.開始用了.
首先進行一個排序的用法
排序就直接用 Array.Sort(people)
然後是尋找相等的array 這裡用Array.BinarySearch
int indexofjimm2 = Array.BinarySearch(people.jimmy2);
這樣就返回桶jimmy2 一樣的jimmy在array的哪個位置.

好了差不多不講了,其實現在asp.net2.0中有更加強大的ArrayList可以用.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.