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
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 位环境中,可以通过将 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
返回 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。 |
适用于
线程安全性
此类型的公共静态(Shared
)成员是线程安全的。 不保证任何实例成员都是线程安全的。
此实现不提供 Array的同步(线程安全)包装器;但是,基于 Array 的 .NET 类使用 SyncRoot 属性提供其自己的集合同步版本。
通过集合进行枚举本质上不是线程安全的过程。 即使集合同步,其他线程仍可以修改集合,这会导致枚举器引发异常。 若要保证枚举期间的线程安全性,可以在整个枚举期间锁定集合,也可以捕获由其他线程所做的更改导致的异常。
另请参阅
- Object
- Type
- 在数组 中执行 Culture-Insensitive 字符串操作
- 数组(C# 编程指南)
- 在 F# 中
数组 - Visual Basic 中的
数组