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数组之间复制元素。
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 是其第一个元素的索引。 一个 Array 可以具有任何下限,但默认情况下其下限为零。 使用
与命名空间中的 System.Collections 类不同, Array 具有固定容量。 若要增加容量,必须创建具有所需容量的新 Array 对象,将元素从旧 Array 对象复制到新对象,然后删除旧 Array对象。
数组大小限制为总共 40 亿个元素,在任何给定维度中的最大索引为 0X7FEFFFFF(对于字节数组和单字节结构的数组0X7FFFFFC7)。
.NET Framework only: 默认情况下,Array 的最大大小为 2 GB。 在 64 位环境中,通过将 gcAllowVeryLargeObjects 配置元素的属性设置为trueenabled运行时环境中,可以避免大小限制。
单维数组实现 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、CopyToGetValue和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) |
返回当前实例的哈希代码。 |
扩展方法
| 名称 | 说明 |
|---|---|
| AsParallel(IEnumerable) |
启用查询的并行化。 |
| AsQueryable(IEnumerable) |
将 IEnumerable 转换为 IQueryable。 |
| Cast<TResult>(IEnumerable) |
将 IEnumerable 的元素强制转换为指定类型。 |
| OfType<TResult>(IEnumerable) |
根据指定类型筛选 IEnumerable 的元素。 |
适用于
线程安全性
此类型的公共静态(Shared 在 Visual Basic 中)成员是线程安全的。 不保证任何实例成员都是线程安全的。
此实现不提供Array的同步(线程安全)包装器;但是,基于 Array 的.NET类使用 SyncRoot 属性提供其自己的集合同步版本。
通过集合进行枚举本质上不是线程安全的过程。 即使集合同步,其他线程仍可以修改集合,这会导致枚举器引发异常。 若要保证枚举期间的线程安全性,可以在整个枚举期间锁定集合,也可以捕获由其他线程所做的更改导致的异常。