Array.Clone Methode

Definition

Erstellt eine flache Kopie von Array.

public:
 System::Object ^ Clone();
public:
 virtual System::Object ^ Clone();
public object Clone ();
public virtual object Clone ();
member this.Clone : unit -> obj
abstract member Clone : unit -> obj
override this.Clone : unit -> obj
Public Function Clone () As Object
Public Overridable Function Clone () As Object

Gibt zurück

Object

Eine flache Kopie von Array.

Implementiert

Beispiele

Im folgenden Codebeispiel wird ein System.Globalization.CultureInfo Array geklont und das Verhalten einer flachen Kopie veranschaulicht.

using namespace System;
using namespace System::Globalization;
void PrintIndexAndValues( Array^ myArray );
int main()
{
   
   // Create and initialize a new CultureInfo array.
   CultureInfo^ ci0 = gcnew CultureInfo( "ar-SA",false );
   CultureInfo^ ci1 = gcnew CultureInfo( "en-US",false );
   CultureInfo^ ci2 = gcnew CultureInfo( "fr-FR",false );
   CultureInfo^ ci3 = gcnew CultureInfo( "ja-JP",false );
   array<CultureInfo^>^arrCI = {ci0,ci1,ci2,ci3};
   
   // Create a clone of the CultureInfo array.
   array<CultureInfo^>^arrCIClone = (array<CultureInfo^>^)arrCI->Clone();
   
   // Replace an element in the clone array.
   CultureInfo^ ci4 = gcnew CultureInfo( "th-TH",false );
   arrCIClone[ 0 ] = ci4;
   
   // Display the contents of the original array.
   Console::WriteLine( "The original array contains the following values:" );
   PrintIndexAndValues( arrCI );
   
   // Display the contents of the clone array.
   Console::WriteLine( "The clone array contains the following values:" );
   PrintIndexAndValues( arrCIClone );
   
   // Display the DateTimeFormatInfo.DateSeparator for the fourth element in both arrays.
   Console::WriteLine( "Before changes to the clone:" );
   Console::WriteLine( "   Original: The DateTimeFormatInfo->DateSeparator for {0} is {1}.", arrCI[ 3 ]->Name, arrCI[ 3 ]->DateTimeFormat->DateSeparator );
   Console::WriteLine( "      Clone: The DateTimeFormatInfo->DateSeparator for {0} is {1}.", arrCIClone[ 3 ]->Name, arrCIClone[ 3 ]->DateTimeFormat->DateSeparator );
   
   // Replace the DateTimeFormatInfo.DateSeparator for the fourth element in the clone array.
   arrCIClone[ 3 ]->DateTimeFormat->DateSeparator = "-";
   
   // Display the DateTimeFormatInfo.DateSeparator for the fourth element in both arrays.
   Console::WriteLine( "After changes to the clone:" );
   Console::WriteLine( "   Original: The DateTimeFormatInfo->DateSeparator for {0} is {1}.", arrCI[ 3 ]->Name, arrCI[ 3 ]->DateTimeFormat->DateSeparator );
   Console::WriteLine( "      Clone: The DateTimeFormatInfo->DateSeparator for {0} is {1}.", arrCIClone[ 3 ]->Name, arrCIClone[ 3 ]->DateTimeFormat->DateSeparator );
}

void PrintIndexAndValues( Array^ myArray )
{
   for ( int i = myArray->GetLowerBound( 0 ); i <= myArray->GetUpperBound( 0 ); i++ )
      Console::WriteLine( "\t[{0}]:\t{1}", i, myArray->GetValue( i ) );
}

/* 
This code produces the following output.

The original array contains the following values:
        [0]:    ar-SA
        [1]:    en-US
        [2]:    fr-FR
        [3]:    ja-JP
The clone array contains the following values:
        [0]:    th-TH
        [1]:    en-US
        [2]:    fr-FR
        [3]:    ja-JP
Before changes to the clone:
   Original: The DateTimeFormatInfo.DateSeparator for ja-JP is /.
      Clone: The DateTimeFormatInfo.DateSeparator for ja-JP is /.
After changes to the clone:
   Original: The DateTimeFormatInfo.DateSeparator for ja-JP is -.
      Clone: The DateTimeFormatInfo.DateSeparator for ja-JP is -.

*/
using System;
using System.Globalization;
public class SamplesArray  {

   public static void Main()  {

      // Create and initialize a new CultureInfo array.
      CultureInfo ci0 = new CultureInfo( "ar-SA", false );
      CultureInfo ci1 = new CultureInfo( "en-US", false );
      CultureInfo ci2 = new CultureInfo( "fr-FR", false );
      CultureInfo ci3 = new CultureInfo( "ja-JP", false );
      CultureInfo[] arrCI = new CultureInfo[] { ci0, ci1, ci2, ci3 };

      // Create a clone of the CultureInfo array.
      CultureInfo[] arrCIClone = (CultureInfo[]) arrCI.Clone();

      // Replace an element in the clone array.
      CultureInfo ci4 = new CultureInfo( "th-TH", false );
      arrCIClone[0] = ci4;

      // Display the contents of the original array.
      Console.WriteLine( "The original array contains the following values:" );
      PrintIndexAndValues( arrCI );

      // Display the contents of the clone array.
      Console.WriteLine( "The clone array contains the following values:" );
      PrintIndexAndValues( arrCIClone );

      // Display the DateTimeFormatInfo.DateSeparator for the fourth element in both arrays.
      Console.WriteLine( "Before changes to the clone:" );
      Console.WriteLine( "   Original: The DateTimeFormatInfo.DateSeparator for {0} is {1}.", arrCI[3].Name, arrCI[3].DateTimeFormat.DateSeparator );
      Console.WriteLine( "      Clone: The DateTimeFormatInfo.DateSeparator for {0} is {1}.", arrCIClone[3].Name, arrCIClone[3].DateTimeFormat.DateSeparator );

      // Replace the DateTimeFormatInfo.DateSeparator for the fourth element in the clone array.
      arrCIClone[3].DateTimeFormat.DateSeparator = "-";

      // Display the DateTimeFormatInfo.DateSeparator for the fourth element in both arrays.
      Console.WriteLine( "After changes to the clone:" );
      Console.WriteLine( "   Original: The DateTimeFormatInfo.DateSeparator for {0} is {1}.", arrCI[3].Name, arrCI[3].DateTimeFormat.DateSeparator );
      Console.WriteLine( "      Clone: The DateTimeFormatInfo.DateSeparator for {0} is {1}.", arrCIClone[3].Name, arrCIClone[3].DateTimeFormat.DateSeparator );
   }

   public static void PrintIndexAndValues( Array myArray )  {
      for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
         Console.WriteLine( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) );
   }
}


/*
This code produces the following output.

The original array contains the following values:
        [0]:    ar-SA
        [1]:    en-US
        [2]:    fr-FR
        [3]:    ja-JP
The clone array contains the following values:
        [0]:    th-TH
        [1]:    en-US
        [2]:    fr-FR
        [3]:    ja-JP
Before changes to the clone:
   Original: The DateTimeFormatInfo.DateSeparator for ja-JP is /.
      Clone: The DateTimeFormatInfo.DateSeparator for ja-JP is /.
After changes to the clone:
   Original: The DateTimeFormatInfo.DateSeparator for ja-JP is -.
      Clone: The DateTimeFormatInfo.DateSeparator for ja-JP is -.

*/
open System
open System.Globalization

let printIndexAndValues (myArray: 'a []) =
    for i = myArray.GetLowerBound 0 to myArray.GetUpperBound 0 do
        printfn $"\t[{i}]:\t{myArray[i]}"

// Create and initialize a new CultureInfo array.
let ci0 = CultureInfo("ar-SA", false)
let ci1 = CultureInfo("en-US", false)
let ci2 = CultureInfo("fr-FR", false)
let ci3 = CultureInfo("ja-JP", false)
let arrCI = [| ci0; ci1; ci2; ci3 |]

// Create a clone of the CultureInfo array.
let arrCIClone = arrCI.Clone() :?> CultureInfo []

// Replace an element in the clone array.
let ci4 = CultureInfo("th-TH", false)
arrCIClone[0] <- ci4

// Display the contents of the original array.
printfn "The original array contains the following values:"
printIndexAndValues arrCI

// Display the contents of the clone array.
printfn "The clone array contains the following values:"
printIndexAndValues arrCIClone

// Display the DateTimeFormatInfo.DateSeparator for the fourth element in both arrays.
printfn "Before changes to the clone:"
printfn $"   Original: The DateTimeFormatInfo.DateSeparator for {arrCI[3].Name} is {arrCI[3].DateTimeFormat.DateSeparator}."
printfn $"      Clone: The DateTimeFormatInfo.DateSeparator for {arrCIClone[3].Name} is {arrCIClone[3].DateTimeFormat.DateSeparator}."

// Replace the DateTimeFormatInfo.DateSeparator for the fourth element in the clone array.
arrCIClone[3].DateTimeFormat.DateSeparator <- "-"

// Display the DateTimeFormatInfo.DateSeparator for the fourth element in both arrays.
printfn "After changes to the clone:"
printfn $"   Original: The DateTimeFormatInfo.DateSeparator for {arrCI[3].Name} is {arrCI[3].DateTimeFormat.DateSeparator}."
printfn $"      Clone: The DateTimeFormatInfo.DateSeparator for {arrCIClone[3].Name} is {arrCIClone[3].DateTimeFormat.DateSeparator}."


// This code produces the following output.
//     The original array contains the following values:
//             [0]:    ar-SA
//             [1]:    en-US
//             [2]:    fr-FR
//             [3]:    ja-JP
//     The clone array contains the following values:
//             [0]:    th-TH
//             [1]:    en-US
//             [2]:    fr-FR
//             [3]:    ja-JP
//     Before changes to the clone:
//        Original: The DateTimeFormatInfo.DateSeparator for ja-JP is /.
//           Clone: The DateTimeFormatInfo.DateSeparator for ja-JP is /.
//     After changes to the clone:
//        Original: The DateTimeFormatInfo.DateSeparator for ja-JP is -.
//           Clone: The DateTimeFormatInfo.DateSeparator for ja-JP is -.
Imports System.Globalization

Public Class SamplesArray

    Public Shared Sub Main()

        ' Create and initialize a new CultureInfo array.
        Dim ci0 As New CultureInfo("ar-SA", False)
        Dim ci1 As New CultureInfo("en-US", False)
        Dim ci2 As New CultureInfo("fr-FR", False)
        Dim ci3 As New CultureInfo("ja-JP", False)
        Dim arrCI() As CultureInfo = {ci0, ci1, ci2, ci3}

        ' Create a clone of the CultureInfo array.
        Dim arrCIClone As CultureInfo() = CType(arrCI.Clone(), CultureInfo())

        ' Replace an element in the clone array.
        Dim ci4 As New CultureInfo("th-TH", False)
        arrCIClone(0) = ci4

        ' Display the contents of the original array.
        Console.WriteLine("The original array contains the following values:")
        PrintIndexAndValues(arrCI)

        ' Display the contents of the clone array.
        Console.WriteLine("The clone array contains the following values:")
        PrintIndexAndValues(arrCIClone)

        ' Display the DateTimeFormatInfo.DateSeparator for the fourth element in both arrays.
        Console.WriteLine("Before changes to the clone:")
        Console.WriteLine("   Original: The DateTimeFormatInfo.DateSeparator for {0} is {1}.", arrCI(3).Name, arrCI(3).DateTimeFormat.DateSeparator)
        Console.WriteLine("      Clone: The DateTimeFormatInfo.DateSeparator for {0} is {1}.", arrCIClone(3).Name, arrCIClone(3).DateTimeFormat.DateSeparator)

        ' Replace the DateTimeFormatInfo.DateSeparator for the fourth element in the clone array.
        arrCIClone(3).DateTimeFormat.DateSeparator = "-"

        ' Display the DateTimeFormatInfo.DateSeparator for the fourth element in both arrays.
        Console.WriteLine("After changes to the clone:")
        Console.WriteLine("   Original: The DateTimeFormatInfo.DateSeparator for {0} is {1}.", arrCI(3).Name, arrCI(3).DateTimeFormat.DateSeparator)
        Console.WriteLine("      Clone: The DateTimeFormatInfo.DateSeparator for {0} is {1}.", arrCIClone(3).Name, arrCIClone(3).DateTimeFormat.DateSeparator)

    End Sub

    Public Shared Sub PrintIndexAndValues(myArray As Array)
        Dim i As Integer
        For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
            Console.WriteLine(vbTab + "[{0}]:" + vbTab + "{1}", i, myArray.GetValue(i))
        Next i
    End Sub

End Class


'This code produces the following output.
'
'The original array contains the following values:
'        [0]:    ar-SA
'        [1]:    en-US
'        [2]:    fr-FR
'        [3]:    ja-JP
'The clone array contains the following values:
'        [0]:    th-TH
'        [1]:    en-US
'        [2]:    fr-FR
'        [3]:    ja-JP
'Before changes to the clone:
'   Original: The DateTimeFormatInfo.DateSeparator for ja-JP is /.
'      Clone: The DateTimeFormatInfo.DateSeparator for ja-JP is /.
'After changes to the clone:
'   Original: The DateTimeFormatInfo.DateSeparator for ja-JP is -.
'      Clone: The DateTimeFormatInfo.DateSeparator for ja-JP is -.

Hinweise

Eine flache Kopie einer Kopie nur Array die Elemente der Elemente , Arrayunabhängig davon, ob es sich um Referenztypen oder Werttypen handelt, aber sie kopiert nicht die Objekte, auf die sich die Verweise beziehen. Die Verweise im neuen Array Punkt auf dieselben Objekte, auf die die Verweise im ursprünglichen Array Punkt verweisen.

Im Gegensatz dazu kopiert eine tiefe Kopie der Array Elemente und alles, was direkt oder indirekt von den Elementen referenziert wird.

Der Klon ist identisch Type mit dem Original Array.

Diese Methode ist ein O()-Vorgang, wobei n es sich um einen O(n)-Vorgang handeltLength.

Gilt für

Siehe auch