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。
using namespace System;
void PrintValues(array<Object^>^myArr);
void PrintValues(array<int>^myArr);
void main()
{
// Creates and initializes a new int array and a new Object array.
array<int>^myIntArray = { 1,2,3,4,5 };
array<Object^>^myObjArray = { 26,27,28,29,30 };
// Prints the initial values of both arrays.
Console::WriteLine("Initially:");
Console::Write("int array: ");
PrintValues(myIntArray);
Console::Write("Object array:");
PrintValues(myObjArray);
// Copies the first two elements from the int 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 int array to the Object array:");
Console::Write("int array: ");
PrintValues(myIntArray);
Console::Write("Object array:");
PrintValues(myObjArray);
// Copies the last two elements from the Object array to the int 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 int array:");
Console::Write("int array: ");
PrintValues(myIntArray);
Console::Write("Object array:");
PrintValues(myObjArray);
}
void PrintValues(array<Object^>^myArr)
{
for (int i = 0; i < myArr->Length; i++)
{
Console::Write("\t{0}", myArr[i]);
}
Console::WriteLine();
}
void PrintValues(array<int>^myArr)
{
for (int i = 0; i < myArr->Length; i++)
{
Console::Write("\t{0}", myArr[i]);
}
Console::WriteLine();
}
/*
This code produces the following output.
Initially:
int array: 1 2 3 4 5
Object array: 26 27 28 29 30
After copying the first two elements of the int array to the Object array:
int 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 int array:
int array: 1 2 3 29 30
Object array: 1 2 28 29 30
*/
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,並顯示其屬性及其專案。
using namespace System;
void PrintValues(Array^ myArr);
void main()
{
// Creates and initializes a new three-dimensional Array instance of type Int32.
Array^ myArr = Array::CreateInstance( Int32::typeid, 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 instance 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 ) );
}
Console::WriteLine( "The Array instance contains the following values:" );
PrintValues( myArr );
}
void PrintValues( Array^ myArr )
{
System::Collections::IEnumerator^ myEnumerator = myArr->GetEnumerator();
int i = 0;
int cols = myArr->GetLength( myArr->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 instance 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 instance 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
*/
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 的下限是其第一個專案的索引。 Array 可以有任何下限,但預設其下限為零。 使用 CreateInstance建立 Array 類別的實例時,可以定義不同的下限。 多維度 Array 可以針對每個維度有不同的界限。 陣列最多可以有32個維度。
不同於 System.Collections 命名空間中的類別,Array 具有固定容量。 若要增加容量,您必須建立具有所需容量的新 Array 物件、將舊的 Array 物件中的專案複製到新的物件,並刪除舊的 Array。
陣列大小限制為總計 40 億個元素,以及任何指定維度中0X7FEFFFFF的最大索引(0X7FFFFFC7位元組陣列和單一位元組結構的陣列)。
僅限 .NET Framework :根據預設,Array 的大小上限為 2 GB(GB)。 在 64 位環境中,您可以將 gcAllowVeryLargeObject s 的 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
會傳回 false
,而 typeof(System.Array).GetElementType
會傳回 null
。
Array.Copy 方法不僅會在相同類型的數位之間,也會在不同類型的標準陣列之間複製元素;它會自動處理類型轉換。
某些方法,例如 CreateInstance、Copy、CopyTo、GetValue和 SetValue,提供接受64位整數做為參數的多載,以容納大型容量陣列。 LongLength 和 GetLongLength 傳回指出陣列長度的64位整數。
不保證排序 Array。 您必須先排序 Array,才能執行需要排序 Array 的作業(例如 BinarySearch)。
不支援在機器碼中使用指標的 Array 物件,而且會針對數種方法擲回 NotSupportedException。
屬性
IsFixedSize |
取得值,指出 Array 是否有固定大小。 |
IsReadOnly |
取得值,指出 Array 是否為唯讀。 |
IsSynchronized |
取得值,指出是否同步存取 Array (線程安全)。 |
Length |
取得 Array所有維度中的項目總數。 |
LongLength |
取得64位整數,表示 Array的所有維度中的項目總數。 |
MaxLength |
取得陣列中可包含的元素數目上限。 |
Rank |
取得 Array的排名(維度數目)。 例如,一維陣列會傳回 1、二維陣列傳回 2 等等。 |
SyncRoot |
取得對象,這個物件可用來同步存取 Array。 |
方法
明確介面實作
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) |
傳回目前實例的哈希碼。 |
擴充方法
Cast<TResult>(IEnumerable) |
將 IEnumerable 的項目轉換成指定的型別。 |
OfType<TResult>(IEnumerable) |
根據指定的型別篩選 IEnumerable 的專案。 |
AsParallel(IEnumerable) |
啟用查詢的平行處理。 |
AsQueryable(IEnumerable) |
將 IEnumerable 轉換成 IQueryable。 |
適用於
執行緒安全性
此類型的公用靜態 (Shared
) 成員是安全線程。 不保證任何實例成員都是安全線程。
此實作不提供 Array的同步處理(線程安全)包裝函式;不過,以 Array 為基礎的 .NET 類別會使用 SyncRoot 屬性提供自己同步處理的集合版本。
透過集合列舉本質上不是安全線程的程式。 即使集合同步處理,其他線程仍然可以修改集合,這會導致列舉值擲回例外狀況。 若要保證列舉期間的線程安全性,您可以在整個列舉期間鎖定集合,或攔截其他線程所做的變更所產生的例外狀況。
另請參閱
- Object
- Type
- 在陣列 中執行 Culture-Insensitive 字串作業
- 陣列 (C# 程式設計手冊)
- F# 中的
陣列 - 在 Visual Basic 中
陣列