1 System.Array介紹
Array類是一個抽象的基類,我們不能用如下方式建立一個Array類的執行個體
Array myArray=new Array();
但它提供了CreateInstance構建數組
Array myArray=Array.CreateInstance(typeof(string),10);
2.System.Array方法
BinarySearch 使用二進位搜尋方法搜尋一維排序數組中的某人值
Clear 將數組中的一組元素設為0或null
CreateInstance 初始化Array類的新執行個體
GetLength 指定給定維數的數組的元素總個數
GetLowerBound 指定給定數組的下界
GetUpperBound 指定給定數組的上界
GetValue 返回給定數組中指定元素的值,該元素可通過指定位置索引來指明
IndexOf 返回給定值在一維數組中第一次出現時的位置索引
Sort 對數組元素進行排序
3.System.Collections 介紹
System.Collections 是一個命名空間,這個命名空間提供一組介面和類,使使用者能夠對一組資料元素執行集合操作
類:
Hashtable:儲存索引值對,這些索引值對是根據鍵編碼來組織的
SortedList:組織對象並允許通過索引或索引值來訪問這些對象
介面:
ICollection 為所有集合定義大小、列舉程式和同步方法
IEnumerator 對整個集合的簡單迴圈和列舉,可以使用派生自IEnumerator 的IDictionaryEnumerator 來枚舉字典中的元素
IList 通過索引進行單獨訪問的對象的集合
結構:
DictionaryEntry 定義可以設定或檢索的字典索引值對
4.Hashtable 類
Hashtable 類將資料作為一組索引值對來儲存,這些索引值對是根據鍵的編碼來組織的
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Collections;namespace BaseConsole{ public partial class HashtableExample : Form { private Hashtable _hashtable = new Hashtable(); public HashtableExample() { InitializeComponent(); } private void btnAdd_Click(object sender, EventArgs e) { try { //將元素添加到雜湊表 _hashtable.Add(txtKey.Text,txtValue.Text); } catch(Exception ex) { MessageBox.Show("產生系統錯誤。");//產生未知錯誤 Application.Exit(); } lstAddedKey.Items.Add(txtKey.Text); } private void lstAddedKey_SelectedIndexChanged(object sender, EventArgs e) { string selectedItem = lstAddedKey.SelectedItem.ToString(); string selectedValue = (string)_hashtable[selectedItem];//檢索對象 txtSelectedValue.Text = selectedValue; } }}
結果:
5.ArrayList 類
Array類屬於System命名空間,ArrayList類屬於System.Collections命名空間
屬性:
Capacity:指定數組列表可以包含的元素個數,預設容量為16
方法:
Contains:檢查給定元素是否屬於數組列表
Insert:將給定元素插入數組列表中指定的索引位置
Remove:從數組列表中移除第一次出現的給定對象
RemoveAt:移除數組列表中特定索引位置的元素
TrimToSize:定義數組列表中實際元素個數
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;/**/using System.Collections;namespace BaseConsole{ public partial class frmArrayListExample : Form { private ArrayList _arrPerson = new ArrayList(); public frmArrayListExample() { InitializeComponent(); } private void btnAdd_Click(object sender, EventArgs e) { _arrPerson.Add(this.txtPersonName.Text); lstMessage.Items.Clear(); lstMessage.Items.Add("數組的容量是:"+this._arrPerson.Capacity.ToString()); lstMessage.Items.Add("數組的元素個數是:"+this._arrPerson.Count.ToString()); lstMessage.Items.Add("---------------------------------------------------"); foreach(string personName in _arrPerson) { lstMessage.Items.Add("元素:"+personName); } lstMessage.Items.Add("---------------------------------------------------"); _arrPerson.TrimToSize(); lstMessage.Items.Add("整理後的數組容量是:"+_arrPerson.Capacity.ToString()); } private void btnSearch_Click(object sender, EventArgs e) { if (_arrPerson.Contains(txtSearchString.Text)) { MessageBox.Show("存在這個元素。"); } else { MessageBox.Show("不存在這個元素。"); } } }}
結果: