1. Array-數組
在C#中定義一個數組的方法一般為:
Data Type [] Array=new DataType [Size];
其中Data Type可以是基礎資料型別 (Elementary Data Type)和物件類型資料,Size是數組元素的個數.
例子定義一個int類型的資料來表示一個班上的人數
int [] classNum=new int[20];
type[] typename=new type[size];
如
int[] a=new int[2];string[] str=new string[5];
實事上我們平常是用int[],string[]...的,此時我們已經建立一個Array數組,只不過我們平常沒有這種意識而已.
(1):type資料類型不能缺;且要統一,而不能是如 int[] a=new Array[];
(2):數組的大小size不能缺,否則c#認為是出錯的,因為數組是一段固定長度的記憶體;
(3):右邊是一個中括弧[],而不是()
數組對象的方法有靜態方法和執行個體方法;
public static int BinarySearch(Array a, object v);
數組a必須是一維而且排好序的;此方法的結果是返回a中與v匹配的第一個元素的索引號.如果沒有匹配元素則返回一個負數-1;
public static int BinarySearch(Array a, object v, IComparer comp);
與前面的方法相似,區別在於comp;用於比較符號;
public static int BinarySearch(Array a, int start,int count, object v)
從a中start開始的count個元素中查詢與v匹配的元素的一個索引號.
public static int BinarySearch(Array a, int start,int count, object v,IComparer comp)
與上一個方法相似.區別在於用比較法補償;
public static void Clear(Array a, int start, int count)
將數組a中從start開始的從count個元素置零;
public virtual object Clone()Copy這個數組;
public static void Copy(Array source,Array dest, int count)
將source中的count個元素賦值到數組dest中.;
public virtual void CopyTo(Array dest, int start)
將調用此方法的數組對象中的start個元素賦值到dest中.
public static Array CreateInstance(Type t, int size)
建立一個有Size個元素的數組,其中元素類型是t;
public static Array CreateInstance(Type t, int size1, int size2)
建立一個二維數組,數組的參數是[size1][size2];數組元素的類型是t;
public static Array CreateInstance(Type t, int size1,int size2, int size3)
建立一個三維數組,元素類型是T,同建立二維數組一樣.;
public static Array CreateInstance(Type t, int[ ] sizes)
建立一個參數sizes維的數組,元素類型是t;
public static Array CreateInstance(Type t, int[ ] sizes,int[ ] startIndexes)
建立一個多維陣列,數組維數是sizes,每一個元素的資料類型是t.數組中每一維數組的開始索引號是startIndexes;
public override bool Equals(object v)
判斷v是否與調用此方法的數組對象是否相同.
public virtual IEnumerator GetEnumerator()
返回數組的枚舉對象
public int GetLength(int dim)
返回多維陣列第dim維的元素的個數
public override int GetHashCode()
返回數組的雜湊碼;
public int GetUpperBound(int dim)
返回多維陣列第dim維的元素的最後一個索引值
public object GetValue(int index) 返回一維數組第index個元素的值
public object GetValue(int idx1, int idx2)和public object GetValue(int idx1, int idx2,int idx3)分別擷取2維和3維數組的某個特殊元素的值.;
public static int IndexOf(Array a, object v) 返回a中與v的值一樣的第一元素的索引值;
public static int IndexOf(Array a, object v,int start)
從a中第start個元素開始,返回與v的值一樣的元素位置;
public static int IndexOf(Array a, object v,int start, int count)
從a中第start個元素開始的count個元素中,返回與v的值一樣的元素位置;
public static int LastIndexOf(Array a, object v,int start, int count)
在a中得第start個元素開始的count個元素中尋找與v相等的元素的索引號
public static void Reverse(Array a) 將數組a所有元素做個逆轉;
public void SetValue(object v, int index) 設定數組對象的第index個元素的值為v;
對象二維和三維數組有相似的函數;public void SetValue(object v, int idx1, int idx2)和public void SetValue(object v, int idx1,int idx2, int idx3)
public static void Sort(Array a) 對數組a進行排序;
二. 定義和初始化一個數組
int[] myIntArray; myIntArray = new int[4]; 利用數組的函數IndexOf() and LastIndexOf()來擷取數組中某一個特定元素的位置;
| 代碼如下 |
複製代碼 |
int[] intArray = {1, 2, 1, 3}; Console.WriteLine("intArray:"); for (int i = 0; i < intArray.Length; i++) { Console.WriteLine("intArray[" + i + "] = " + intArray[i]);} int index = Array.IndexOf(intArray, 1); Console.WriteLine("Array.IndexOf(intArray, 1) = " + index); index = Array.LastIndexOf(intArray, 1); Console.WriteLine("Array.LastIndexOf(intArray, 1) = " + index); |
二,C# ArrayList數組的用法範例:
| 代碼如下 |
複製代碼 |
ArrayList al = new ArrayList(); for (int i = 0; i < 3; i++) { al.Add(i); Response.Write(al[i].ToString() + "<br>");//輸出數組中的元素值 // Response.Write(al[i]+ "<br>"); } Response.Write(al.Count + "<br><br>"); foreach (int obj in al)//或foreach (object obj in al),因為al是一個object類型的數組 { Response.Write(obj+"-OK"+"<br>"); } |
arrayList利用enumerator來訪問數組:
| 代碼如下 |
複製代碼 |
ArrayList list = new ArrayList(1); for (int i = 0; i < 10; i++) list.Add(i); IEnumerator etr = list.GetEnumerator();//枚舉 while (etr.MoveNext()) Console.Write(etr.Current + " "); |
運行結果:
對了,要想在C#中利用ArrayList必須在系統命名空間中加入一個引用,否則會出現錯誤的。using System.Collections;
下面看一些動態數組的常用方法:
| 代碼如下 |
複製代碼 |
ArrayList list = new ArrayList(5); //構造一個動態數組 // list.Clear();//清楚數組中所有的元素 Console.WriteLine(); Console.WriteLine("there are {0} elements in the list", list.Count); ArrayList shuzu = new ArrayList(5); shuzu.Add("仙劍一"); shuzu.Add("亮劍"); shuzu.Add("bianceng."); shuzu.Add("國家寶藏"); shuzu.Add("陸小鳳"); for (int x = 0; x <= shuzu.Count - 1; x++) Console.WriteLine(shuzu[x]); Console.WriteLine("now reverse itn"); shuzu.Reverse(); Console.WriteLine("after reversern"); Console.WriteLine(); for (int x = 0; x <= shuzu.Count - 1; x++) Console.WriteLine(shuzu[x]); |
三,ArrayList和Array相互之間的轉化
(1)以下是把ArrayList數組中的值拷貝到Array中去的執行個體用法
| 代碼如下 |
複製代碼 |
//int[] IResult=new int[al.Count]; //al.CopyTo(IResult); //或是用下面的方法 int[] IResult = (int[])al.ToArray(typeof(Int32));//ToArray(Int32);這樣錯誤,一定要強制類型轉換 //Person[] personArrayFromList = (Person[])personList.ToArray(typeof(Person)); foreach(int item in IResult) { Response.Write(item.ToString()); } Response.Write("以下是把Array數組中的值拷貝到ArrayList中去的執行個體用法" + "<br>" + "結果為:<br>"); int[] a ={ 222, 333, 555 }; ArrayList arrList = new ArrayList(); foreach (object obj in a)//或foreach (int obj in a) { arrList.Add(obj); Response.Write(obj+"<br>"); } |