1 Array
(1) 提供創建、操作、搜索和排序數組的方法,因而在公共語言運行庫中用作所有數組的基類。
(2)public abstract class Array : ICloneable, IList, ICollection, IEnumerable
(3)Array 類是支持數組的語言實現的基類。但是,只有系統和編譯器能夠從 Array 類顯式派生。用戶應當使用由語言提供的數組構造。
一個元素就是 Array 中的一個值。Array 的長度是它可包含的元素總數。Array 的秩是 Array 中的維數。Array 中維度的下限是 Array 中該維度的起始索引,多維 Array 的各個維度可以有不同的界限。
(4)重要事項:在 .NET Framework 2.0 版中,Array 類實現 System.Collections.Generic.IList、System.Collections.Generic.ICollection 和 System.Collections.Generic.IEnumerable 泛型接口。由於實現是在運行時提供給數組的,因而對於文檔生成工具不可見。因此,泛型接口不會出現在 Array 類的聲明語法中,也不會有關於只能通過將數組強制轉換為泛型接口類型(顯式接口實現)才可訪問的接口成員的參考主題。將某一數組強制轉換為這三種接口之一時需要注意的關鍵一點是,添加、插入或移除元素的成員會引發 NotSupportedException。
(5)Type 對象提供有關數組類型聲明的信息。具有相同數組類型的 Array 對象共享同一 Type 對象。
(6)Type.IsArray 和 Type.GetElementType 可能不返回所預期的 Array 形式的結果,因為如果某個數組被強制轉換為 Array 類型,則結果是對象,而非數組。即,typeof(System.Array).IsArray 返回 false,而 typeof(System.Array).GetElementType 返回 空引用(在 Visual Basic 中為 Nothing)。
(7)與大多數類不同,Array 提供 CreateInstance 方法,以便允許后期綁定訪問,而不是提供公共構造函數。
(8)Array.Copy 方法不僅可在同一類型的數組之間復制元素,而且可在不同類型的標准數組之間復制元素;它會自動處理強制類型轉換。
(9)有些方法,如 CreateInstance、Copy、CopyTo、GetValue 和 SetValue,提供重載(接受 64 位整數作為參數),以適應大容量數組。LongLength 和 GetLongLength 返回指示數組長度的 64 位整數。
(10)不保證會對 Array 進行排序。在執行需要對 Array 進行排序的操作(如 BinarySearch)之前,必須對 Array 進行排序。
2 下面的代碼示例說明 Array.Copy 如何在 integer 類型的數組和 Object 類型的數組之間復制元素。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.IO; namespace Array { class Program { public static void PrintValues(Object[] myArr) //打印對象數組 { foreach (Object i in myArr) { Console.Write("\t{0}", i); } Console.WriteLine(); } public static void PrintValues(int[] myArr) //打印整形數組 { foreach (int i in myArr) { Console.Write("\t{0}", i); } Console.WriteLine(); } static void Main(string[] args) { // Creates and initializes a new integer array and a new Object array. int[] myIntArray = new int[5] { 1, 2, 3, 4, 5 }; Object[] myObjArray = new Object[5] { 26, 27, 28, 29, 30 }; // Prints the initial values of both arrays. Console.WriteLine("Initially,"); Console.Write("integer array:"); PrintValues(myIntArray); Console.Write("Object array: "); PrintValues(myObjArray); // Copies the first two elements from the integer array to the Object array. System.Array.Copy(myIntArray, myObjArray, 2); // Prints the values of the modified arrays. Console.WriteLine("\nAfter copying the first two elements of the integer array to the Object array,"); Console.Write("integer array:"); PrintValues(myIntArray); Console.Write("Object array: "); PrintValues(myObjArray); // Copies the last two elements from the Object array to the integer array. System.Array.Copy(myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, myIntArray.GetUpperBound(0) - 1, 2); //該數組是一維的,所以用GetUpperBound(0)得到第0維的上限 // Prints the values of the modified arrays. Console.WriteLine("\nAfter copying the last two elements of the Object array to the integer array,"); Console.Write("integer array:"); PrintValues(myIntArray); Console.Write("Object array: "); PrintValues(myObjArray); Console.Read(); } } }
3 下面的代碼示例創建並初始化一個 Array,然后顯示其屬性和元素。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
namespace Sample
{
class Program
{
public static void PrintValues(Array myArr)
{
System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
int i = 0;//用來控制換行
int cols = myArr.GetLength(myArr.Rank - 1);
//myRank是3維,GetLength是從第0維開始的
while (myEnumerator.MoveNext())
{
if (i < cols)
{
i++;
}
else
{
Console.WriteLine();
i = 1;
}
Console.Write("\t{0}", myEnumerator.Current);
}
Console.WriteLine();
}
static void Main(string[] args)
{
// Creates and initializes a new three-dimensional Array of type Int32.
Array myArr = Array.CreateInstance(typeof(Int32), 2, 3, 4);
for (int i = myArr.GetLowerBound(0); i <= myArr.GetUpperBound(0); i++)
for (int j = myArr.GetLowerBound(1); j <= myArr.GetUpperBound(1); j++)
for (int k = myArr.GetLowerBound(2); k <= myArr.GetUpperBound(2); k++)
{
myArr.SetValue((i * 100) + (j * 10) + k, i, j, k);
}
// Displays the properties of the Array.
Console.WriteLine("The Array has {0} dimension(s) and a total of {1} elements.", myArr.Rank, myArr.Length);
Console.WriteLine("\tLength\tLower\tUpper");
for (int i = 0; i < myArr.Rank; i++)
{
Console.Write("{0}:\t{1}", i, myArr.GetLength(i));
Console.WriteLine("\t{0}\t{1}", myArr.GetLowerBound(i), myArr.GetUpperBound(i));
}
// Displays the contents of the Array.
Console.WriteLine("The Array contains the following values:");
PrintValues(myArr);
Console.Read();
}
}
}