Array.Copy 方法

定义

将一个 Array 的一部分元素复制到另一个 Array 中,并根据需要执行类型转换和装箱。

重载

Copy(Array, Int64, Array, Int64, Int64)

复制 Array 中的一系列元素(从指定的源索引开始),并将它们粘贴到另一 Array 中(从指定的目标索引开始)。 长度和索引指定为 64 位整数。

Copy(Array, Int32, Array, Int32, Int32)

复制 Array 中的一系列元素(从指定的源索引开始),并将它们粘贴到另一 Array 中(从指定的目标索引开始)。 长度和索引指定为 32 位整数。

Copy(Array, Array, Int32)

从第一个元素开始复制 Array 中的一系列元素,将它们粘贴到另一 Array 中(从第一个元素开始)。 长度指定为 32 位整数。

Copy(Array, Array, Int64)

从第一个元素开始复制 Array 中的一系列元素,将它们粘贴到另一 Array 中(从第一个元素开始)。 长度指定为 64 位整数。

示例

下面的代码示例演示如何从类型之一Array复制到整数类型的另一个ArrayObject

using namespace System;
void PrintValues( Array^ myArr );
void main()
{
   
   // Creates and initializes a new Array instance of type Int32.
   Array^ myIntArray = Array::CreateInstance( Type::GetType(  "System.Int32" ), 5 );
   for ( int i = myIntArray->GetLowerBound( 0 ); i <= myIntArray->GetUpperBound( 0 ); i++ )
      myIntArray->SetValue( i + 1, i );
   
   // Creates and initializes a new Array instance of type Object.
   Array^ myObjArray = Array::CreateInstance( Type::GetType(  "System.Object" ), 5 );
   for ( int i = myObjArray->GetLowerBound( 0 ); i <= myObjArray->GetUpperBound( 0 ); i++ )
      myObjArray->SetValue( i + 26, i );
   
   // Displays the initial values of both arrays.
   Console::WriteLine(  "Int32 array:" );
   PrintValues( myIntArray );
   Console::WriteLine(  "Object array:" );
   PrintValues( myObjArray );
   
   // Copies the first element from the Int32 array to the Object array.
   Array::Copy( myIntArray, myIntArray->GetLowerBound( 0 ), myObjArray, myObjArray->GetLowerBound( 0 ), 1 );
   
   // Copies the last two elements from the Object array to the Int32 array.
   Array::Copy( myObjArray, myObjArray->GetUpperBound( 0 ) - 1, myIntArray, myIntArray->GetUpperBound( 0 ) - 1, 2 );
   
   // Displays the values of the modified arrays.
   Console::WriteLine(  "Int32 array - Last two elements should now be the same as Object array:" );
   PrintValues( myIntArray );
   Console::WriteLine(  "Object array - First element should now be the same as Int32 array:" );
   PrintValues( myObjArray );
}

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.
 
 Int32 array:
     1    2    3    4    5
 Object array:
     26    27    28    29    30
 Int32 array - Last two elements should now be the same as Object array:
     1    2    3    29    30
 Object array - First element should now be the same as Int32 array:
     1    27    28    29    30
 */
open System

let printValues (myArr: 'a []) =
    let mutable i = 0;
    let cols = myArr.GetLength(myArr.Rank - 1)
    for item in myArr do
        if i < cols then
            i <- i + 1
        else
            printfn ""
            i <- 1
        printf $"\t{item}"
    printfn ""

 // Creates and initializes a new Array of type int.
let myIntArray = [| 1..5 |]

// Creates and initializes a new Array of type Object.
let myObjArray = Array.init 5 (fun i -> i + 26 :> obj)

// Displays the initial values of both arrays.
printfn "int array:"
printValues myIntArray 
printfn "Object array:"
printValues myObjArray

// Copies the first element from the int array to the Object array.
Array.Copy(myIntArray, myIntArray.GetLowerBound 0, myObjArray, myObjArray.GetLowerBound 0, 1)

// Copies the last two elements from the Object array to the int array.
Array.Copy(myObjArray, myObjArray.GetUpperBound 0 - 1, myIntArray, myIntArray.GetUpperBound 0 - 1, 2)

// Displays the values of the modified arrays.
printfn "int array - Last two elements should now be the same as Object array:"
printValues myIntArray 
printfn "Object array - First element should now be the same as int array:"
printValues myObjArray


// This code produces the following output.
//     int array:
//         1    2    3    4    5
//     Object array:
//         26    27    28    29    30
//     int array - Last two elements should now be the same as Object array:
//         1    2    3    29    30
//     Object array - First element should now be the same as int array:
//         1    27    28    29    30
using System;
public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a new Array of type int.
      Array myIntArray=Array.CreateInstance( typeof(System.Int32), 5 );
      for ( int i = myIntArray.GetLowerBound(0); i <= myIntArray.GetUpperBound(0); i++ )
         myIntArray.SetValue( i+1, i );

      // Creates and initializes a new Array of type Object.
      Array myObjArray = Array.CreateInstance( typeof(System.Object), 5 );
      for ( int i = myObjArray.GetLowerBound(0); i <= myObjArray.GetUpperBound(0); i++ )
         myObjArray.SetValue( i+26, i );

      // Displays the initial values of both arrays.
      Console.WriteLine( "int array:" );
      PrintValues( myIntArray );
      Console.WriteLine( "Object array:" );
      PrintValues( myObjArray );

      // Copies the first element from the int array to the Object array.
      Array.Copy( myIntArray, myIntArray.GetLowerBound(0), myObjArray, myObjArray.GetLowerBound(0), 1 );

      // Copies the last two elements from the Object array to the int array.
      Array.Copy( myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, myIntArray.GetUpperBound(0) - 1, 2 );

      // Displays the values of the modified arrays.
      Console.WriteLine( "int array - Last two elements should now be the same as Object array:" );
      PrintValues( myIntArray );
      Console.WriteLine( "Object array - First element should now be the same as int array:" );
      PrintValues( myObjArray );
   }

   public static 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.

int array:
    1    2    3    4    5
Object array:
    26    27    28    29    30
int array - Last two elements should now be the same as Object array:
    1    2    3    29    30
Object array - First element should now be the same as int array:
    1    27    28    29    30
*/
Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Array of type Int32.
        Dim myIntArray As Array = _
           Array.CreateInstance(GetType(System.Int32), 5)
        Dim i As Integer
        For i = myIntArray.GetLowerBound(0) To myIntArray.GetUpperBound(0)
            myIntArray.SetValue(i + 1, i)
        Next i 
        ' Creates and initializes a new Array of type Object.
        Dim myObjArray As Array = _
           Array.CreateInstance(GetType(System.Object), 5)
        For i = myObjArray.GetLowerBound(0) To myObjArray.GetUpperBound(0)
            myObjArray.SetValue(i + 26, i)
        Next i 
        ' Displays the initial values of both arrays.
        Console.WriteLine("Int32 array:")
        PrintValues(myIntArray)
        Console.WriteLine("Object array:")
        PrintValues(myObjArray)
        
        ' Copies the first element from the Int32 array to the Object array.
        Array.Copy(myIntArray, myIntArray.GetLowerBound(0), myObjArray, _
           myObjArray.GetLowerBound(0), 1)
        
        ' Copies the last two elements from the Object array to the Int32 array.
        Array.Copy(myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, _
           myIntArray.GetUpperBound(0) - 1, 2)
        
        ' Displays the values of the modified arrays.
        Console.WriteLine("Int32 array - Last two elements should now be " _
           + "the same as Object array:")
        PrintValues(myIntArray)
        Console.WriteLine("Object array - First element should now be the " _
           + "same as Int32 array:")
        PrintValues(myObjArray)
    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.
' 
' Int32 array:
'     1    2    3    4    5
' Object array:
'     26    27    28    29    30
' Int32 array - Last two elements should now be the same as Object array:
'     1    2    3    29    30
' Object array - First element should now be the same as Int32 array:
'     1    27    28    29    30

Copy(Array, Int64, Array, Int64, Int64)

复制 Array 中的一系列元素(从指定的源索引开始),并将它们粘贴到另一 Array 中(从指定的目标索引开始)。 长度和索引指定为 64 位整数。

public:
 static void Copy(Array ^ sourceArray, long sourceIndex, Array ^ destinationArray, long destinationIndex, long length);
public static void Copy (Array sourceArray, long sourceIndex, Array destinationArray, long destinationIndex, long length);
static member Copy : Array * int64 * Array * int64 * int64 -> unit
Public Shared Sub Copy (sourceArray As Array, sourceIndex As Long, destinationArray As Array, destinationIndex As Long, length As Long)

参数

sourceArray
Array

包含要复制的数据的 Array

sourceIndex
Int64

一个 64 位整数,它表示复制开始处的 sourceArray 中的索引。

destinationArray
Array

接收数据的 Array

destinationIndex
Int64

一个 64 位整数,它表示存储开始处的 destinationArray 中的索引。

length
Int64

一个 64 位整数,它表示要复制的元素数目。 整数必须介于零和 Int32.MaxValue 之间(含)。

例外

sourceArraynull

destinationArraynull

sourceArraydestinationArray 具有不同的秩。

sourceArraydestinationArray 属于不兼容的类型。

sourceArray 中至少有一个元素无法转换为 destinationArray 的类型。

sourceIndex 超出了 sourceArray 的有效索引范围。

- 或 -

destinationIndex 超出了 destinationArray 的有效索引范围。

- 或 -

length 小于 0 或大于 Int32.MaxValue

length 大于从 sourceIndexsourceArray 末尾的元素数。

- 或 -

length 大于从 destinationIndexdestinationArray 末尾的元素数。

注解

sourceArraydestinationArray 参数必须具有相同的维度数。 此外, destinationArray 必须已进行维度调整,并且必须具有从位置开始 destinationIndex 的足够数量的元素,以容纳复制的数据。

在多维数组之间复制时,数组的行为类似于一个长一维数组,其中 (行或列) 在概念上是端到端布局的。 例如,如果数组有三行 (或列) 各有四个元素,则从数组开头复制六个元素将复制第一行 (或列) 的所有四个元素,第二行的前两个元素 (或列) 。 若要从第三行 (或列) 的第二个元素开始复制, sourceIndex 必须是第一行 (或列) 的上限加上第二行 (或列的长度) 加 2。

如果 sourceArraydestinationArray 重叠,此方法的行为就像在覆盖之前destinationArray在临时位置保留 的原始值sourceArray一样。

[C++]

此方法等效于标准 C/C++ 函数 memmove,而不是 memcpy

数组可以是引用类型数组或值类型数组。 根据需要执行类型向下转换。

  • 从引用类型数组复制到值类型数组时,每个元素将被取消装箱,然后复制。 从值类型数组复制到引用类型数组时,将装箱并复制每个元素。

  • 从引用类型或值类型数组复制到 Object 数组时,将创建 以 Object 保存每个值或引用,然后复制。 从 Object 数组复制到引用类型或值类型数组且无法赋值时, InvalidCastException 将引发 。

  • 如果 sourceArraydestinationArray 都是引用类型数组,或者都是 类型的 Object数组,则执行浅表复制。 的 Array 浅表副本是包含 Array 对与原始 Array相同的元素的新引用。 不会复制元素本身或元素引用的任何内容。 相比之下,的深层副本 Array 会复制元素以及元素直接或间接引用的所有内容。

ArrayTypeMismatchException如果数组的类型不兼容,则会引发 。 类型兼容性定义如下:

  • 类型与自身兼容。

  • 值类型与 Object 和 与由该值类型实现的接口类型兼容。 仅当值类型直接实现该接口时,它才被视为连接到接口。 断开连接的类型不兼容。

  • 如果从源类型复制到目标类型是扩展转换,则两个内部 (预定义) 值类型是兼容的。 扩大转换永远不会丢失信息,而缩小转换可能会丢失信息。 例如,将 32 位有符号整数转换为 64 位有符号整数是一种扩大转换,将 64 位有符号整数转换为 32 位有符号整数是一种缩小的转换。 有关转换的详细信息,请参阅 Convert

  • 非内部 (用户定义的) 值类型仅与自身兼容。

  • 枚举具有到 Enum 和 的隐式转换,以转换为其基础类型。

如果 中的每个sourceArray元素都需要向下转换 (例如,从基类到派生类或从接口到对象) 并且一个或多个元素不能强制转换为 中的destinationArrayInvalidCastException相应类型。

如果此方法在复制时引发异常,则 状态 destinationArray 为未定义。

此方法是 O (n) 操作,其中 nlength

另请参阅

适用于

Copy(Array, Int32, Array, Int32, Int32)

复制 Array 中的一系列元素(从指定的源索引开始),并将它们粘贴到另一 Array 中(从指定的目标索引开始)。 长度和索引指定为 32 位整数。

public:
 static void Copy(Array ^ sourceArray, int sourceIndex, Array ^ destinationArray, int destinationIndex, int length);
public static void Copy (Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length);
static member Copy : Array * int * Array * int * int -> unit
Public Shared Sub Copy (sourceArray As Array, sourceIndex As Integer, destinationArray As Array, destinationIndex As Integer, length As Integer)

参数

sourceArray
Array

包含要复制的数据的 Array

sourceIndex
Int32

一个 32 位整数,它表示 sourceArray 中复制开始处的索引。

destinationArray
Array

接收数据的 Array

destinationIndex
Int32

一个 32 位整数,它表示 destinationArray 中存储开始处的索引。

length
Int32

一个 32 位整数,它表示要复制的元素数目。

例外

sourceArraynull

destinationArraynull

sourceArraydestinationArray 具有不同的秩。

sourceArraydestinationArray 属于不兼容的类型。

sourceArray 中至少有一个元素无法转换为 destinationArray 的类型。

sourceIndex 少于 sourceArray 的第一个维度的下限。

- 或 -

destinationIndex 少于 destinationArray 的第一个维度的下限。

- 或 -

length 小于零。

length 大于从 sourceIndexsourceArray 末尾的元素数。

- 或 -

length 大于从 destinationIndexdestinationArray 末尾的元素数。

注解

sourceArraydestinationArray 参数必须具有相同的维度数。 此外, destinationArray 必须已进行维度调整,并且必须具有从位置开始 destinationIndex 的足够数量的元素,以容纳复制的数据。

在多维数组之间复制时,数组的行为类似于一个长一维数组,其中 (行或列) 在概念上是端到端布局的。 例如,如果数组有三行 (或列) 各有四个元素,则从数组开头复制六个元素将复制第一行 (或列) 的所有四个元素,第二行的前两个元素 (或列) 。 若要从第三行 (或列) 的第二个元素开始复制, sourceIndex 必须是第一行 (或列) 的上限加上第二行 (或列的长度) 加 2。

如果 sourceArraydestinationArray 重叠,此方法的行为就像在覆盖之前destinationArray在临时位置保留 的原始值sourceArray一样。

[C++]

此方法等效于标准 C/C++ 函数 memmove,而不是 memcpy

数组可以是引用类型数组或值类型数组。 根据需要执行类型向下转换。

  • 从引用类型数组复制到值类型数组时,每个元素将被取消装箱,然后复制。 从值类型数组复制到引用类型数组时,将装箱并复制每个元素。

  • 从引用类型或值类型数组复制到 Object 数组时,将创建 以 Object 保存每个值或引用,然后复制。 从 Object 数组复制到引用类型或值类型数组且无法赋值时, InvalidCastException 将引发 。

  • 如果 sourceArraydestinationArray 都是引用类型数组,或者都是 类型的 Object数组,则执行浅表复制。 的 Array 浅表副本是包含 Array 对与原始 Array相同的元素的新引用。 不会复制元素本身或元素引用的任何内容。 相比之下,的深层副本 Array 会复制元素以及元素直接或间接引用的所有内容。

ArrayTypeMismatchException如果数组的类型不兼容,则会引发 。 类型兼容性定义如下:

  • 类型与自身兼容。

  • 值类型与 Object 和 与由该值类型实现的接口类型兼容。 仅当值类型直接实现该接口时,它才被视为连接到接口。 断开连接的类型不兼容。

  • 如果从源类型复制到目标类型是扩展转换,则两个内部 (预定义) 值类型是兼容的。 扩大转换永远不会丢失信息,而缩小转换可能会丢失信息。 例如,将 32 位有符号整数转换为 64 位有符号整数是一种扩大转换,将 64 位有符号整数转换为 32 位有符号整数是一种缩小的转换。 有关转换的详细信息,请参阅 Convert

  • 非内部 (用户定义的) 值类型仅与自身兼容。

  • 枚举具有到 Enum 和 的隐式转换,以转换为其基础类型。

如果 中的每个sourceArray元素都需要向下转换 (例如,从基类到派生类或从接口到对象) 并且一个或多个元素不能强制转换为 中的destinationArrayInvalidCastException相应类型。

如果此方法在复制时引发异常,则 状态 destinationArray 为未定义。

此方法是 O (n) 操作,其中 nlength

另请参阅

适用于

Copy(Array, Array, Int32)

从第一个元素开始复制 Array 中的一系列元素,将它们粘贴到另一 Array 中(从第一个元素开始)。 长度指定为 32 位整数。

public:
 static void Copy(Array ^ sourceArray, Array ^ destinationArray, int length);
public static void Copy (Array sourceArray, Array destinationArray, int length);
static member Copy : Array * Array * int -> unit
Public Shared Sub Copy (sourceArray As Array, destinationArray As Array, length As Integer)

参数

sourceArray
Array

包含要复制的数据的 Array

destinationArray
Array

接收数据的 Array

length
Int32

一个 32 位整数,它表示要复制的元素数目。

例外

sourceArraynull

destinationArraynull

sourceArraydestinationArray 具有不同的秩。

sourceArraydestinationArray 属于不兼容的类型。

sourceArray 中至少有一个元素无法转换为 destinationArray 的类型。

length 小于零。

length 大于 sourceArray 中的元素数。

- 或 -

length 大于 destinationArray 中的元素数。

注解

sourceArraydestinationArray 参数必须具有相同的维度数。 此外, destinationArray 必须已进行维度调整,并且必须具有足够数量的元素来容纳复制的数据。

在多维数组之间复制时,数组的行为类似于一个长一维数组,其中 (行或列) 在概念上是端到端放置的。 例如,如果数组有三行 (或列) 各有四个元素,则从数组开头复制六个元素将复制第一行 (或列) 的所有四个元素,第二行的前两个元素 (或列) 。

如果 sourceArraydestinationArray 重叠,此方法的行为就像在覆盖之前destinationArray在临时位置保留 的原始值sourceArray一样。

[C++]

此方法等效于标准 C/C++ 函数 memmove,而不是 memcpy

数组可以是引用类型数组或值类型数组。 根据需要执行类型向下转换。

  • 从引用类型数组复制到值类型数组时,每个元素将被取消装箱,然后复制。 从值类型数组复制到引用类型数组时,将装箱并复制每个元素。

  • 从引用类型或值类型数组复制到 Object 数组时,将创建 以 Object 保存每个值或引用,然后复制。 从 Object 数组复制到引用类型或值类型数组且无法赋值时, InvalidCastException 将引发 。

  • 如果 sourceArraydestinationArray 都是引用类型数组,或者都是 类型的 Object数组,则执行浅表复制。 的 Array 浅表副本是包含 Array 对与原始 Array相同的元素的新引用。 不会复制元素本身或元素引用的任何内容。 相比之下,的深层副本 Array 会复制元素以及元素直接或间接引用的所有内容。

ArrayTypeMismatchException如果数组的类型不兼容,则会引发 。 类型兼容性定义如下:

  • 类型与自身兼容。

  • 值类型与 Object 和 与由该值类型实现的接口类型兼容。 仅当值类型直接实现该接口时,它才被视为连接到接口。 断开连接的类型不兼容。

  • 如果从源类型复制到目标类型是扩展转换,则两个内部 (预定义) 值类型是兼容的。 扩大转换永远不会丢失信息,而缩小转换可能会丢失信息。 例如,将 32 位有符号整数转换为 64 位有符号整数是一种扩大转换,将 64 位有符号整数转换为 32 位有符号整数是一种缩小的转换。 有关转换的详细信息,请参阅 Convert

  • 非内部 (用户定义的) 值类型仅与自身兼容。

  • 枚举具有到 Enum 和 的隐式转换,以转换为其基础类型。

如果 中的每个sourceArray元素都需要向下转换 (例如,从基类到派生类或从接口到对象) 并且一个或多个元素不能强制转换为 中的destinationArrayInvalidCastException相应类型。

如果此方法在复制时引发异常,则 状态 destinationArray 为未定义。

此方法是 O (n) 操作,其中 nlength

另请参阅

适用于

Copy(Array, Array, Int64)

从第一个元素开始复制 Array 中的一系列元素,将它们粘贴到另一 Array 中(从第一个元素开始)。 长度指定为 64 位整数。

public:
 static void Copy(Array ^ sourceArray, Array ^ destinationArray, long length);
public static void Copy (Array sourceArray, Array destinationArray, long length);
static member Copy : Array * Array * int64 -> unit
Public Shared Sub Copy (sourceArray As Array, destinationArray As Array, length As Long)

参数

sourceArray
Array

包含要复制的数据的 Array

destinationArray
Array

接收数据的 Array

length
Int64

一个 64 位整数,它表示要复制的元素数目。 整数必须介于零和 Int32.MaxValue 之间(含)。

例外

sourceArraynull

destinationArraynull

sourceArraydestinationArray 具有不同的秩。

sourceArraydestinationArray 属于不兼容的类型。

sourceArray 中至少有一个元素无法转换为 destinationArray 的类型。

length 小于 0 或大于 Int32.MaxValue

length 大于 sourceArray 中的元素数。

- 或 -

length 大于 destinationArray 中的元素数。

注解

sourceArraydestinationArray 参数必须具有相同的维度数。 此外, destinationArray 必须已进行维度调整,并且必须具有足够数量的元素来容纳复制的数据。

在多维数组之间复制时,数组的行为类似于一个长一维数组,其中 (行或列) 在概念上是端到端放置的。 例如,如果数组有三行 (或列) 各有四个元素,则从数组开头复制六个元素将复制第一行 (或列) 的所有四个元素,第二行的前两个元素 (或列) 。

如果 sourceArraydestinationArray 重叠,此方法的行为就像在覆盖之前destinationArray在临时位置保留 的原始值sourceArray一样。

[C++]

此方法等效于标准 C/C++ 函数 memmove,而不是 memcpy

数组可以是引用类型数组或值类型数组。 根据需要执行类型向下转换。

  • 从引用类型数组复制到值类型数组时,每个元素将被取消装箱,然后复制。 从值类型数组复制到引用类型数组时,将装箱并复制每个元素。

  • 从引用类型或值类型数组复制到 Object 数组时, Object 将创建 以保存每个值或引用,然后复制。 从 Object 数组复制到引用类型或值类型数组且无法进行赋值时, InvalidCastException 将引发 。

  • 如果 sourceArraydestinationArray 都是引用类型的数组,或者都是 类型的 Object数组,则执行浅表复制。 的Array浅表副本是包含对与原始 Array相同的元素的引用的新Array。 不会复制元素本身或元素引用的任何内容。 相比之下,的 Array 深层副本会复制元素以及元素直接或间接引用的所有内容。

ArrayTypeMismatchException如果数组的类型不兼容,则会引发 。 类型兼容性定义如下:

  • 类型与自身兼容。

  • 值类型与 Object 和 与由该值类型实现的接口类型兼容。 仅当值类型直接实现该接口时,它才被视为已连接到接口。 断开连接的类型不兼容。

  • 如果从源类型复制到目标类型是扩大转换,则两个内部 (预定义) 值类型是兼容的。 扩大转换永远不会丢失信息,而收缩转换可能会丢失信息。 例如,将 32 位有符号整数转换为 64 位有符号整数是一种扩大转换,将 64 位有符号整数转换为 32 位有符号整数是一种收缩转换。 有关转换的详细信息,请参阅 Convert

  • 非内部 (用户定义的) 值类型仅与自身兼容。

  • 枚举具有到 Enum 和 的隐式转换,以转换为其基础类型。

如果 中的每个 sourceArray 元素都需要向下转换 (例如,从基类到派生类,或者从接口到对象) 并且一个或多个元素无法强制转换为 中的 destinationArray相应类型, InvalidCastException 则会引发 。

如果此方法在复制时引发异常,则 的状态 destinationArray 为未定义。

此方法是 O (n) 操作,其中 nlength

另请参阅

适用于