Array 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供一些方法,用于创建、处理、搜索数组并对数组进行排序,从而充当公共语言运行时中所有数组的基类。
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
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Array = class
interface ICloneable
interface ICollection
interface IList
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 亿个元素,对于字节数组和单字节结构数组) ,任何给定维度 (0X7FFFFFC7的最大索引为 0X7FEFFFFF。
仅限.NET Framework:默认情况下,的最大大小Array为 2 GB (GB) 。 在 64 位环境中,可以通过在运行时环境中将 gcAllowVeryLargeObjects 配置元素的 属性设置为 enabled
来true
避免大小限制。
一维数组实现 System.Collections.Generic.IList<T>、System.Collections.Generic.ICollection<T>、 System.Collections.Generic.IReadOnlyList<T>System.Collections.Generic.IEnumerable<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 ,然后才能执行 (操作,例如 BinarySearch 需要 Array 排序的) 。
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。 |
适用于
线程安全性
Visual Basic 中的公共静态 (Shared
) 此类型的成员是线程安全的。 但不保证所有实例成员都是线程安全的。
此实现不会为 Array提供同步 (线程安全的) 包装器;但是,基于 Array .NET 类使用 SyncRoot 属性提供其自己的集合同步版本。
枚举整个集合本质上不是一个线程安全的过程。 即使某个集合已同步,其他线程仍可以修改该集合,这会导致枚举数引发异常。 若要确保枚举过程中的线程安全性,可以在整个枚举期间锁定集合,或者捕获由其他线程进行的更改所导致的异常。