ArrayList及Hashtable示例

ArrayList

  System.Collections.ArrayList類是一個特殊的數組。通過添加和刪除元素,就可以動態改變數組的長度。

一.優點

1。支持自動改變大小的功能

2。可以靈活的插入元素

3。可以靈活的刪除元素

二.侷限性

跟一般的數組比起來,速度上差些

三.添加元素

1.  public virtual int Add(object value);

將對象添加到 ArrayList 的結尾處

ArrayList aList = new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

內容爲a b c d e

2.  public virtual void Insert(int index,object value);

  將元素插入 ArrayList 的指定索引處

ArrayList aList = new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.Insert(0,"aa");

結果爲aa a b  c d e

3.  public virtual void InsertRange(int index,ICollection c);

   將集合中的某個元素插入 ArrayList 的指定索引處

  ArrayList aList = new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

ArrayList list2 = new ArrayList();

        list2.Add("tt");

list2.Add("ttt");

aList.InsertRange(2,list2);

結果爲a b tt ttt c d e

四.刪除

a)       public virtual void Remove(object obj);

   從 ArrayList 中移除特定對象的第一個匹配項,注意是第一個

ArrayList aList = new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.Remove("a");

結果爲b c d e

2. public virtual void RemoveAt(int index);

移除 ArrayList 的指定索引處的元素

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.RemoveAt(0);

結果爲b c d e

3.  public virtual void RemoveRange(int index,int count);

從 ArrayList 中移除一定範圍的元素。

Index表示索引,count表示從索引處開始的數目

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.RemoveRange(1,3);

結果爲a e

4.  public virtual void Clear();

從 ArrayList 中移除所有元素。

五.排序

a)       public virtual void Sort();

對 ArrayList 或它的一部分中的元素進行排序。

ArrayList aList = new ArrayList();

aList.Add("e");

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

DropDownList1.DataSource = aList;  // DropDownList DropDownList1;

DropDownList1.DataBind();

結果爲e a b c d

ArrayList aList = new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.Sort();  //排序

DropDownList1.DataSource = aList;  // DropDownList DropDownList1;

DropDownList1.DataBind();

結果爲a b c d e

b)       public virtual void Reverse();

將 ArrayList 或它的一部分中元素的順序反轉。

ArrayList aList = new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.Reverse();  //反轉

DropDownList1.DataSource = aList;  // DropDownList DropDownList1;

DropDownList1.DataBind();

結果爲 e d c b a

六.查找

a)       public virtual int IndexOf(object);

b)       public virtual int IndexOf(object, int);

c)       public virtual int IndexOf(object, int, int);

  返回 ArrayList 或它的一部分中某個值的第一個匹配項的從零開始的索引。沒找到返回-1。

ArrayList aList = new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

int nIndex = aList.IndexOf(“a”);  //1

nIndex = aList.IndexOf(“p”);     //沒找到,-1

d)       public virtual int LastIndexOf(object);

e)         public virtual int LastIndexOf(object, int);

f)         public virtual int LastIndexOf(object, int, int);

  返回 ArrayList 或它的一部分中某個值的最後一個匹配項的從零開始的索引。

ArrayList aList = new ArrayList();

aList.Add("a"); 

aList.Add("b");

aList.Add("a");  //同0

aList.Add("d");

aList.Add("e");

int nIndex = aList.LastIndexOf("a");  //值爲2而不是0

g)       public virtual bool Contains(object item);

確定某個元素是否在 ArrayList 中。包含返回true,否則返回false

七.其他

1.public virtual int Capacity {get; set;}

獲取或設置 ArrayList 可包含的元素數。

2.public virtual int Count {get;}

獲取 ArrayList 中實際包含的元素數。

Capacity 是 ArrayList 可以存儲的元素數。Count 是 ArrayList 中實際包含的元素數。Capacity 總是大於或等於 Count。如果在添加元素時,Count 超過 Capacity,則該列表的容量會通過自動重新分配內部數組加倍。

如果 Capacity 的值顯式設置,則內部數組也需要重新分配以容納指定的容量。如果 Capacity 被顯式設置爲 0,則公共語言運行庫將其設置爲默認容量。默認容量爲 16。

在調用Clear後,Count爲0,而此時Capacity切是默認容量16,而不是0

3.public virtual void TrimToSize();

將容量設置爲 ArrayList 中元素的實際數量。

如果不向列表中添加新元素,則此方法可用於最小化列表的內存系統開銷。

若要完全清除列表中的所有元素,請在調用 TrimToSize 之前調用 Clear 方法。截去空 ArrayList 會將 ArrayList 的容量設置爲默認容量,而不是零。

ArrayList aList = new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");  //Count = 5,Capacity=16,

aList.TrimToSize();  //Count=Capacity=5;

作者:  來源:CSDN  (責任編輯:webjx)  

Hashtable

 

 

 

 

 

一。介紹

 

 

 

 

 

   表示鍵/值對的集合,這些鍵/值對 根據鍵的哈希代碼進行組織。

 

 

 

 

 

   提供快速的查詢。元素的存儲與順序無關。不能在指定位置插入元素,因爲它本身沒有有效的排序。感覺它的優點體現在查詢上。

 

 

 

 

 

   hashtable的鍵必須是唯一的,沒有有效的排序,它進行的是內在的排序

 

 

 

 

 

 

 

 

 

 

 

public class Hashtable : IDictionary, ICollection, IEnumerable,

 

 

 

 

 

 

   ISerializable, IDeserializationCallback, ICloneable

 

 

 

 

 

 

每個元素是一個存儲在 DictionaryEntry 對象中的鍵/值對。鍵不能爲空引用(Visual Basic 中爲 Nothing),但值可以。

 

 

 

 

 

 

注:所謂的DictionaryEntry 結構,就是定義可設置或檢索的字典鍵值對,有一個Key屬性,一個Value屬性,分別代表鍵和值 

 

 

 

 

 

 

二。添加

 

 

 

 

 

Hashtable.Add 方法

 

 

 

 

 

public virtual void Add(    object key,   object value );

 

 

 

 

 

 

將帶有指定鍵和值的元素添加到 Hashtable 中。

 

 

 

 

 

key 要添加的元素的鍵。value 要添加的元素的值。該值可以爲空引用(Visual Basic 中爲 Nothing)。

 

 

 

 

 

 

通過設置 Hashtable 中不存在的鍵的值,Item 屬性也可用於添加新元素。例如:myCollection["myNonexistentKey"] = myValue。但是,如果指定的鍵已經存在於 Hashtable 中,設置 Item 屬性將改寫舊值。相比之下,Add 方法不修改現有元素。

Hashtable hTable = new Hashtable();

 

 

 

 

 

 

hTable.Add("1","a");

 

 

 

 

 

 

hTable.Add("2","b");

 

 

 

 

 

 

hTable.Add("3","c");

 

 

 

 

 

 

hTable.Add("4","d");

 

 

 

 

 

 

hTable.Add("5","e");

 

 

 

 

 

 

//hTable.Add("1","aaaaaaaaaaa");  //出錯,因爲已經有鍵“1

 

 

 

 

 

hTable["1"] = "aaaaaaaaaaa";  //ok

 

 

 

 

 

 

DropDownList2.DataSource = hTable;

 

 

 

 

 

 

DropDownList2.DataTextField = "Key";

 

 

 

 

 

 

DropDownList2.DataValueField = "Value";

 

 

 

 

 

 

DropDownList2.DataBind(); 

 

 

 

 

 

 

二。刪除

 

 

 

 

 

1public virtual void Remove(   object key );

 

 

 

 

 

 

Hashtable 中移除帶有指定鍵的元素。

 

 

 

 

 

如果 Hashtable 不包含帶有指定鍵的元素,則 Hashtable 保持不變。不引發異常。

 

 

 

 

 

 

2public virtual void Clear(); Hashtable 中移除所有元素。

 

 

 

 

 

Count 設置爲零。容量保持不變。

 

 

 

 

 

 

 

 

 

 

 

三。其他

 

 

 

 

 

1

public virtual ICollection Values {get;}

 

 

 

 

 

 

獲取包含 Hashtable 中的值的 ICollection

 

 

 

 

 

注:ICollection 接口,定義所有集合的大小、枚舉數和同步方法。

 

 

 

 

 

Hashtable hTable = new Hashtable();

 

 

 

 

 

 

hTable.Add("1","a");

 

 

 

 

 

 

hTable.Add("2","b");

 

 

 

 

 

 

hTable.Add("3","c");

 

 

 

 

 

 

hTable.Add("4","d");

 

 

 

 

 

 

hTable.Add("5","e");

 

 

 

 

 

 

Label2.Text = "";

 

 

 

 

 

 

foreach (string strKey in hTable.Keys)  //遍歷

 

 

 

 

 

{

 

 

 

 

 

 

Label2.Text += strKey;

 

 

 

 

 

 

Label2.Text += ",";

 

 

 

 

 

 

}

 

 

 

 

 

 

 

 

 

 

 

 

2

public virtual ICollection Keys {get;}

 

 

 

 

 

 

獲取包含 Hashtable 中的鍵的 ICollection

 

 

 

 

 

返回的 ICollection 不是靜態副本;相反,ICollection 反向引用原始 Hashtable 中的鍵。

 

 

 

 

 

 

 

 

 

 

因此,對 Hashtable 的更改繼續反映到 ICollection 中。

 

 

 

 

 

 

 

 

 

 

 

3public virtual bool Contains(    object key );

 

 

 

 

 

 

確定 Hashtable 是否包含特定鍵。 

 

 

 

 

 

 

4public virtual bool ContainsKey(    object key );

 

 

 

 

 

 

確定 Hashtable 是否包含特定鍵。與Contains 

 

 

 

 

 

 

5public virtual bool ContainsValue(   object value );

 

 

 

 

 

 

確定 Hashtable 是否包含特定值

 

 

 

 

 

 

 

 

 

 

 

6

public virtual int Count {get;}

 

 

 

 

 

 

獲取包含在 Hashtable 中的鍵值對的數目  

Hashtable示例
<%@ Page Language="C#" AutoEventWireup="True" Debug="true" %>
<s cript language="C#" runat="server">
void Page_Load(Object Sender,EventArgs e){
 Hashtable HT_values=new Hashtable();
 HT_values.Add("1234","Microsoft");
 HT_values.Add("3210","IBM");
 HT_values.Add("4321","SUN");
 HT_values.Add("5623","Orlce");
 
 MyDownList.DataSource=HT_values;
 MyDownList.DataValueField="Key";
 MyDownList.DataTextField="Value";
 
 MyListBox.DataSource=HT_values;
 MyListBox.DataValueField="Key";
 MyListBox.DataTextField="Value";
 
 MyDataGrid.DataSource=HT_values;
 
 Page.DataBind();
}
</s cript>
<form id="Form_1" runat="server">
<asp:dropdownlist ID="MyDownList" runat="server"></asp:dropdownlist><br>
<asp:listbox ID="MyListBox" runat="server"></asp:listbox><br>
<asp:datagrid ID="MyDataGrid" runat="server" AutoGenerateColumns="false">
 <columns>
  <asp:boundcolumn HeaderText="Key" DataField="Key"/>
  <asp:boundcolumn HeaderText="Value" DataField="Value"/>
 </columns>
</asp:datagrid>
</form>

 

 

發佈了10 篇原創文章 · 獲贊 4 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章