다음을 통해 공유


Array.Clone 메서드

Array의 단순 복사본을 만듭니다.

네임스페이스: System
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
Public Function Clone As Object
‘사용 방법
Dim instance As Array
Dim returnValue As Object

returnValue = instance.Clone
public Object Clone ()
public:
virtual Object^ Clone () sealed
public final Object Clone ()
public final function Clone () : Object

반환 값

Array의 단순 복사본입니다.

설명

Array의 단순 복사본은 참조 형식 또는 값 형식에 관계 없이 Array의 요소만을 복사하지만 참조에서 참조하는 개체는 복사하지 않습니다. 새 Array의 참조는 원래 Array의 참조에서 가리키는 개체와 동일한 개체를 가리킵니다.

이와 반대로 Array의 전체 복사본은 요소와 해당 요소가 직접 또는 간접적으로 참조하는 모든 대상을 복사합니다.

복제본은 원래 Array와 동일한 Type으로 되어 있습니다.

이 메서드는 O(n) 연산이며, 여기서 n은 Length입니다.

예제

다음 코드 예제에서는 System.Globalization.CultureInfo 배열을 복제하고 단순 복사본의 동작을 보여 줍니다.

Imports System
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 'Main

    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 'PrintIndexAndValues 

End Class 'SamplesArray


'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 -.

*/
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 -.

*/
import System.*;
import System.Globalization.*;

public class SamplesArray
{
    public static void main(String[] args)
    {
        // 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.set_Item(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].get_Name(),
            arrCI[3].get_DateTimeFormat().get_DateSeparator());
        Console.WriteLine("      Clone: The DateTimeFormatInfo.DateSeparator " 
            + "for {0} is {1}.", arrCIClone[3].get_Name(),
            arrCIClone[3].get_DateTimeFormat().get_DateSeparator());
        // Replace the DateTimeFormatInfo.DateSeparator for the fourth element
        // in the clone array.
        arrCIClone[3].get_DateTimeFormat().set_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].get_Name(),
            arrCI[3].get_DateTimeFormat().get_DateSeparator());
        Console.WriteLine("      Clone: The DateTimeFormatInfo.DateSeparator " 
            + "for {0} is {1}.", arrCIClone[3].get_Name(),
            arrCIClone[3].get_DateTimeFormat().get_DateSeparator());
    } //main

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

/* 
    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 -.
*/

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

Array 클래스
Array 멤버
System 네임스페이스
Copy
ConstrainedCopy