Array 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供建立、操作、搜尋和排序數位的方法,藉此做為 Common Language Runtime 中所有數位的基類。
public ref class Array abstract : System::Collections::IList, System::Collections::IStructuralComparable, System::Collections::IStructuralEquatable
public ref class Array abstract : ICloneable, System::Collections::IList, System::Collections::IStructuralComparable, System::Collections::IStructuralEquatable
public ref class Array abstract : ICloneable, System::Collections::IList
public abstract class Array : System.Collections.IList, System.Collections.IStructuralComparable, System.Collections.IStructuralEquatable
public abstract class Array : ICloneable, System.Collections.IList, System.Collections.IStructuralComparable, System.Collections.IStructuralEquatable
[System.Serializable]
public abstract class Array : ICloneable, System.Collections.IList
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Array : ICloneable, System.Collections.IList
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Array : ICloneable, System.Collections.IList, System.Collections.IStructuralComparable, System.Collections.IStructuralEquatable
type Array = class
interface ICollection
interface IEnumerable
interface IList
interface IStructuralComparable
interface IStructuralEquatable
type Array = class
interface ICollection
interface IEnumerable
interface IList
interface IStructuralComparable
interface IStructuralEquatable
interface ICloneable
[<System.Serializable>]
type Array = class
interface ICloneable
interface IList
interface ICollection
interface IEnumerable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Array = class
interface ICloneable
interface IList
interface ICollection
interface IEnumerable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Array = class
interface ICloneable
interface IList
interface ICollection
interface IEnumerable
interface IStructuralComparable
interface IStructuralEquatable
type Array = class
interface IList
interface ICollection
interface IEnumerable
interface IStructuralComparable
interface IStructuralEquatable
Public MustInherit Class Array
Implements IList, IStructuralComparable, IStructuralEquatable
Public MustInherit Class Array
Implements ICloneable, IList, IStructuralComparable, IStructuralEquatable
Public MustInherit Class Array
Implements ICloneable, IList
- 繼承
-
Array
- 屬性
- 實作
範例
以下程式碼範例說明如何在 Array.Copy 型態為整數的陣列與型別 Object為 的陣列之間複製元素。
open System
let printValues myArr =
for i in myArr do
printf $"\t{i}"
printfn ""
// Creates and initializes a new integer array and a new Object array.
let myIntArray = [| 1..5 |]
let myObjArray = [| 26..30 |]
// Prints the initial values of both arrays.
printfn "Initially,"
printf "integer array:"
printValues myIntArray
printfn "Object array: "
printValues myObjArray
// Copies the first two elements from the integer array to the Object array.
Array.Copy(myIntArray, myObjArray, 2)
// Prints the values of the modified arrays.
printfn "\nAfter copying the first two elements of the integer array to the Object array,"
printf "integer array:"
printValues myIntArray
printf"Object array: "
printValues myObjArray
// Copies the last two elements from the Object array to the integer array.
Array.Copy(myObjArray, myObjArray.GetUpperBound 0 - 1, myIntArray, myIntArray.GetUpperBound 0 - 1, 2)
// Prints the values of the modified arrays.
printfn $"\nAfter copying the last two elements of the Object array to the integer array,"
printf "integer array:"
printValues myIntArray
printf "Object array: "
printValues myObjArray
// This code produces the following output.
// Initially,
// integer array: 1 2 3 4 5
// Object array: 26 27 28 29 30
//
// After copying the first two elements of the integer array to the Object array,
// integer array: 1 2 3 4 5
// Object array: 1 2 28 29 30
//
// After copying the last two elements of the Object array to the integer array,
// integer array: 1 2 3 29 30
// Object array: 1 2 28 29 30
using System;
public class SamplesArray
{
public static void Main()
{
// 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);
// 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);
}
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();
}
}
/*
This code produces the following output.
Initially,
integer array: 1 2 3 4 5
Object array: 26 27 28 29 30
After copying the first two elements of the integer array to the Object array,
integer array: 1 2 3 4 5
Object array: 1 2 28 29 30
After copying the last two elements of the Object array to the integer array,
integer array: 1 2 3 29 30
Object array: 1 2 28 29 30
*/
Public Class SamplesArray
Public Shared Sub Main()
' Creates and initializes a new integer array and a new Object array.
Dim myIntArray() As Integer = {1, 2, 3, 4, 5}
Dim myObjArray() As Object = {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(ControlChars.NewLine + "After 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)
' Prints the values of the modified arrays.
Console.WriteLine(ControlChars.NewLine + "After 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)
End Sub
Public Overloads Shared Sub PrintValues(myArr() As Object)
Dim i As Object
For Each i In myArr
Console.Write(ControlChars.Tab + "{0}", i)
Next i
Console.WriteLine()
End Sub
Public Overloads Shared Sub PrintValues(myArr() As Integer)
Dim i As Integer
For Each i In myArr
Console.Write(ControlChars.Tab + "{0}", i)
Next i
Console.WriteLine()
End Sub
End Class
' This code produces the following output.
'
' Initially:
' integer array: 1 2 3 4 5
' Object array: 26 27 28 29 30
'
' After copying the first two elements of the integer array to the Object array:
' integer array: 1 2 3 4 5
' Object array: 1 2 28 29 30
'
' After copying the last two elements of the Object array to the integer array:
' integer array: 1 2 3 29 30
' Object array: 1 2 28 29 30
以下程式碼範例建立並初始化 , Array 並顯示其屬性與元素。
open System
let printValues (myArray: Array) =
let mutable i = 0
let cols = myArray.GetLength(myArray.Rank - 1)
for item in myArray do
if i < cols then
i <- i + 1
else
printfn ""
i <- 1;
printf $"\t{item}"
printfn ""
// Creates and initializes a new three-dimensional Array of type int.
let myArr = Array.CreateInstance(typeof<int>, 2, 3, 4)
for i = myArr.GetLowerBound 0 to myArr.GetUpperBound 0 do
for j = myArr.GetLowerBound 1 to myArr.GetUpperBound 1 do
for k = myArr.GetLowerBound 2 to myArr.GetUpperBound 2 do
myArr.SetValue(i * 100 + j * 10 + k, i, j, k)
// Displays the properties of the Array.
printfn $"The Array has {myArr.Rank} dimension(s) and a total of {myArr.Length} elements."
printfn $"\tLength\tLower\tUpper"
for i = 0 to myArr.Rank - 1 do
printf $"{i}:\t{myArr.GetLength i}"
printfn $"\t{myArr.GetLowerBound i}\t{myArr.GetUpperBound i}"
// Displays the contents of the Array.
printfn "The Array contains the following values:"
printValues myArr
// This code produces the following output.
// The Array has 3 dimension(s) and a total of 24 elements.
// Length Lower Upper
// 0: 2 0 1
// 1: 3 0 2
// 2: 4 0 3
//
// The Array contains the following values:
// 0 1 2 3
// 10 11 12 13
// 20 21 22 23
// 100 101 102 103
// 110 111 112 113
// 120 121 122 123
// Creates and initializes a new three-dimensional Array of type int.
Array myArr = Array.CreateInstance(typeof(int), 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);
void PrintValues(Array myArray)
{
System.Collections.IEnumerator myEnumerator = myArray.GetEnumerator();
int i = 0;
int cols = myArray.GetLength(myArray.Rank - 1);
while (myEnumerator.MoveNext())
{
if (i < cols)
{
i++;
}
else
{
Console.WriteLine();
i = 1;
}
Console.Write("\t{0}", myEnumerator.Current);
}
Console.WriteLine();
}
// This code produces the following output.
// The Array has 3 dimension(s) and a total of 24 elements.
// Length Lower Upper
// 0: 2 0 1
// 1: 3 0 2
// 2: 4 0 3
//
// The Array contains the following values:
// 0 1 2 3
// 10 11 12 13
// 20 21 22 23
// 100 101 102 103
// 110 111 112 113
// 120 121 122 123
Public Class SamplesArray2
Public Shared Sub Main()
' Creates and initializes a new three-dimensional Array of
' type Int32.
Dim myArr As Array = Array.CreateInstance(GetType(Int32), 2, 3, 4)
Dim i As Integer
For i = myArr.GetLowerBound(0) To myArr.GetUpperBound(0)
Dim j As Integer
For j = myArr.GetLowerBound(1) To myArr.GetUpperBound(1)
Dim k As Integer
For k = myArr.GetLowerBound(2) To myArr.GetUpperBound(2)
myArr.SetValue(i * 100 + j * 10 + k, i, j, k)
Next k
Next j
Next i ' 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(ControlChars.Tab + "Length" + ControlChars.Tab _
+ "Lower" + ControlChars.Tab + "Upper")
For i = 0 To myArr.Rank - 1
Console.Write("{0}:" + ControlChars.Tab + "{1}", i,
myArr.GetLength(i))
Console.WriteLine(ControlChars.Tab + "{0}" + ControlChars.Tab _
+ "{1}", myArr.GetLowerBound(i), myArr.GetUpperBound(i))
Next i
' Displays the contents of the Array.
Console.WriteLine("The Array contains the following values:")
PrintValues(myArr)
End Sub
Public Shared Sub PrintValues(myArr As Array)
Dim myEnumerator As System.Collections.IEnumerator =
myArr.GetEnumerator()
Dim i As Integer = 0
Dim cols As Integer = myArr.GetLength(myArr.Rank - 1)
While myEnumerator.MoveNext()
If i < cols Then
i += 1
Else
Console.WriteLine()
i = 1
End If
Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
End While
Console.WriteLine()
End Sub
End Class
' This code produces the following output.
'
' The Array has 3 dimension(s) and a total of 24 elements.
' Length Lower Upper
' 0: 2 0 1
' 1: 3 0 2
' 2: 4 0 3
' The Array contains the following values:
' 0 1 2 3
' 10 11 12 13
' 20 21 22 23
' 100 101 102 103
' 110 111 112 113
' 120 121 122 123
備註
該 Array 類別不屬於 System.Collections 命名空間。 然而,它仍被視為集合,因為它基於介面。IList
這個 Array 類別是支援陣列的語言實作的基底類別。 然而,只有系統與編譯器能明確從該 Array 類別衍生。 用戶應採用語言所提供的數組建構。
元素是 中的 Array一個值。 的 Array 長度是它能包含的元素總數。 的 Array 下界是其第一個元素的指標。 An Array 可以有任意下界,但預設下界為零。 當使用 CreateInstance該類別建立實例Array時,可以定義不同的下界。 多維 Array 空間的每個維度可以有不同的界限。 陣列最多可以有32個維度。
與命名空間中的 System.Collections 類別不同,擁有 Array 固定容量。 要增加容量,你必須建立 Array 一個具備所需容量的新物件,將舊 Array 物件的元素複製到新物件,並刪除舊 Array物件。
陣列大小限制為總計 40 億個元素,以及任何指定維度中0X7FEFFFFF的最大索引(0X7FFFFFC7位元組陣列和單一位元組結構的陣列)。
.NET僅限框架:預設情況下,Array 的最大大小為 2 GB。 在 64 位元環境中,你可以透過在執行時環境中將 gcAllowVeryLargeObjects 配置元素的屬性設enabled為 來true避免大小限制。
單維陣列實作 System.Collections.Generic.IList<T>了 、 System.Collections.Generic.ICollection<T>、 System.Collections.Generic.IEnumerable<T>及 System.Collections.Generic.IReadOnlyList<T>System.Collections.Generic.IReadOnlyCollection<T> 通用介面。 這些實作會在執行時提供給陣列,因此通用介面不會出現在類別的 Array 宣告語法中。 此外,只有將數位轉換成泛型介面類型(明確介面實作),才能存取介面成員的參考主題。 當你將陣列投射到這些介面時,關鍵要注意的是,加入、插入或移除元素的成員會拋出 NotSupportedException。
Type 物件提供陣列類型宣告的資訊。 Array 擁有相同陣列類型的物件會共用同一 Type 物件。
Type.IsArray 且 Type.GetElementType 可能無法回傳預期結果, Array 因為若將陣列鑄造為型別 Array,結果是物件而非陣列。 也就是說,返回 typeof(System.Array).IsArray ,返回 typeof(System.Array).GetElementTypenull。false
此 Array.Copy 方法不僅複製相同類型陣列間的元素,也能在不同類型的標準陣列間複製;它能自動處理類型鑄造。
有些方法,如 CreateInstance、 Copy、 CopyToGetValueSetValue,提供過載,接受 64 位元整數作為參數,以容納大容量陣列。 LongLength 並 GetLongLength 回傳 64 位元整數,表示陣列長度。
Array不保證會排序 。 你必須先排序Array,才能執行需要排序的Array操作(例如 BinarySearch)。
在原生程式碼中使用 Array 指標物件並不支援,且會對多個方法拋出 a NotSupportedException 。
屬性
| 名稱 | Description |
|---|---|
| IsFixedSize |
會得到一個值,表示 是否 Array 具有固定大小。 |
| IsReadOnly |
取得值,指出 Array 是否為唯讀。 |
| IsSynchronized |
取得值,指出是否同步存取 Array (線程安全)。 |
| Length |
得到 所有維度 Array中元素的總數。 |
| LongLength |
得到一個 64 位元的整數,代表所有維度 Array中元素的總數。 |
| MaxLength |
取得陣列中可包含的元素數目上限。 |
| Rank |
得到 的 Array秩(維度數)。 例如,一維陣列會傳回 1、二維陣列傳回 2 等等。 |
| SyncRoot |
取得一個物件,可用來同步存取 Array。 |
方法
明確介面實作
| 名稱 | Description |
|---|---|
| ICollection.Count |
得到包含於 的 Array元素數量。 |
| ICollection.IsSynchronized |
取得值,這個值表示是否同步存取 Array (安全線程)。 |
| ICollection.SyncRoot |
取得一個物件,可用來同步存取 Array。 |
| IList.Add(Object) |
呼叫此方法總是拋 NotSupportedException 出例外。 |
| IList.Clear() |
移除所有項目。IList |
| IList.Contains(Object) |
判斷專案是否在 IList中。 |
| IList.IndexOf(Object) |
決定特定項目的 IList索引。 |
| IList.Insert(Int32, Object) |
在指定的索引處插入一個項目 IList 。 |
| IList.IsFixedSize |
會得到一個表示是否 Array 固定大小的值。 |
| IList.IsReadOnly |
會得到一個值,表示 是否 Array 為唯讀。 |
| IList.Item[Int32] |
取得或設定位於指定索引處的專案。 |
| IList.Remove(Object) |
移除特定物件 IList首次出現的 。 |
| IList.RemoveAt(Int32) |
移除 IList 指定索引的項目。 |
| IStructuralComparable.CompareTo(Object, IComparer) |
判斷目前的集合物件在之前、發生於與 相同的位置,或依排序順序追蹤另一個物件。 |
| IStructuralEquatable.Equals(Object, IEqualityComparer) |
判斷物件是否等於目前的實例。 |
| IStructuralEquatable.GetHashCode(IEqualityComparer) |
傳回目前實例的哈希碼。 |
擴充方法
| 名稱 | Description |
|---|---|
| AsParallel(IEnumerable) |
啟用查詢的平行處理。 |
| AsQueryable(IEnumerable) |
將 IEnumerable 轉換成 IQueryable。 |
| Cast<TResult>(IEnumerable) |
將 IEnumerable 的項目轉換成指定的型別。 |
| OfType<TResult>(IEnumerable) |
根據指定的型別篩選 IEnumerable 的專案。 |
適用於
執行緒安全性
此類型的公用靜態 (Shared) 成員是安全線程。 不保證任何實例成員都是安全線程。
此實作未提供同步(執行緒安全)包裝器以Array;然而,基於Array的.NET類別會利用SyncRoot特性提供自己的同步版本集合。
透過集合列舉本質上不是安全線程的程式。 即使集合同步處理,其他線程仍然可以修改集合,這會導致列舉值擲回例外狀況。 若要保證列舉期間的線程安全性,您可以在整個列舉期間鎖定集合,或攔截其他線程所做的變更所產生的例外狀況。