C# 數組
數組是一種資料結構,它包含若干相同類型的變數。數組是使用型別宣告的:type[] arrayName;
下面的樣本建立一維、多維和交錯數組:
數組概述
數組具有以下屬性:
數組可以是一維、多維或交錯的。
數值數組元素的預設值設定為零,而引用元素的預設值設定為 null。
交錯數組是數組的數組,因此,它的元素是參考型別,初始化為 null。交錯數組元素的維度和大小可以不同。
數組的索引從零開始:具有 n 個元素的數組的索引是從 0 到 n-1。
數組元素可以是任何類型,包括數群組類型。
數群組類型是從抽象基底類型 Array 派生的參考型別。由於此類型實現了 IEnumerable 和 IEnumerable,因此可以對 C# 中的所有數組使用 foreach 迭代。
在 C# 中,數組實際上是對象,而不只是像 C 和 C++ 中那樣的可定址連續記憶體地區。
System.Array 類提供了許多其他有用的方法和屬性,用於排序、搜尋和複製數組。(mscorlib.system.array對象查看)
| 方法名 |
描述 |
重載 |
| Array.Length |
擷取數組的長度 |
|
| Array.Rank |
顯示數組的維數 |
|
| Array.GetUpperBound(int dimension) |
擷取 System.Array 的指定維度上限。參數 dimension: Array 的從零開始的維度 |
|
| GetLowerBound(int dimension) |
下限 |
|
| object GetValue(int index1, int index2) |
返回索引值 |
多重 |
| SetValue(object value, int index) |
|
|
| IndexOf<T>(T[] array, T value) |
搜尋指定的T類型的對象,並返回整個 Array 中第一個匹配項的索引。<T>必需重載Object的 Equals 比較方法 |
多重 |
| LastIndexOf<T>(T[] array, T value) |
搜尋指定的T類型的對象,並返回整個 Array 中最後一個匹配項的索引。 |
多重 |
| Sort<T>(T[] array) |
使用Array的每個元素的System.IComparable<T>泛型介面實現,對整個Array中的元素進行排序。<T>必需實現IComparable介面 |
多重 |
| ForEach<T>(T[] array, System.Action<T> action) |
|
多重 |
| 更多在對象查看器:mscorlib.system.arra中 |
|
|
對數組使用 foreach(支援多維)
int[] numbers = { 4, 5, 6, 1, 2, 3, -2, -1, 0 };
foreach (int i in numbers)
{
System.Console.WriteLine(i);
}
數組作為參數傳遞
調用數組參數的方法定義
void PrintArray(int[] arr)
{
//code
}
傳遞
可以將初始化的一維數組傳遞給方法。例如:
PrintArray(theArray);
也可以在一個步驟中初始化並傳遞新數組。例如:
void PrintArray(new int[] { 1, 3, 5, 7, 9 })
使用 ref 和 out 傳遞數組時,ref 要初始化, out 可以不初始化。
ArrayList 和 List 數組
兩者是較為複雜的數組,引用 System.Collections 命名空間
ArrayList 或 List 對象是較為複雜的數組。ArrayList 類和 List 泛型類提供多數 System.Collections 類都提供的但 Array 類未提供的一些功能。例如:
Array 的容量是固定的,而 ArrayList 或 List 的容量可根據需要自動擴充。如果更改了 Capacity 屬性的值,則可以自動進行記憶體重新分配和元素複製。
ArrayList 和 List 提供添加、插入或移除某一範圍元素的方法。在 Array 中,您只能一次擷取或設定一個元素的值。
使用 Synchronized 方法很容易建立 ArrayList 或 List 的同步版本。Array 將實現同步的任務留給了使用者。
ArrayList 和 List 提供將唯讀和固定大小封裝返回到集合的方法;而 Array 不提供。
另一方面,Array 提供了 ArrayList 和 List 所缺少的某些靈活性。例如:
可以設定 Array 的下限,但 ArrayList 或 List 的下限始終為零。
Array 可以具有多個維度,而 ArrayList 或 List 始終只是一維的。
特定類型(不包括 Object)的 Array 的效能優於 ArrayList,這是因為 ArrayList 的元素屬於 Object 類型,所以在儲存或檢索實值型別時通常發生裝箱和unboxing操作。不過,在不需要重新分配時(即最初的容量十分接近列表的最大容量),List 的效能與同類型的數組十分相近。
需要數組的大多數情況都可以改為使用 ArrayList 或 List;它們更容易使用,並且一般與相同類型的數組具有相近的效能。
Array 位於 System 命名空間中;ArrayList 位於 System.Collections 命名空間中;List 位於 System.Collections.Generic 命名空間中。
class TestArraysClass
{
static void Main()
{
// Declare a single-dimensional array
int[] array1 = new int[5];
// Declare and set array element values
int[] array2 = new int[] { 1, 3, 5, 7, 9 };
// Alternative syntax
int[] array3 = { 1, 2, 3, 4, 5, 6 };
// Declare a two dimensional array
int[,] multiDimensionalArray1 = new int[2, 3];
// Declare and set array element values
int[,] multiDimensionalArray2 = { { 1, 2, 3 }, { 4, 5, 6 } };
// Declare a jagged array
int[][] jaggedArray = new int[6][];
// Set the values of the first array in the jagged array structure
jaggedArray[0] = new int[4] { 1, 2, 3, 4 };
}
}