Array.Clone メソッドとは? わかりやすく解説

Array.Clone メソッド

Array簡易コピー作成します

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

解説解説

Array簡易コピーでは、要素参照型であるか値型であるかに関係なく、その Array要素だけがコピーされ、それらの参照指している先のオブジェクトコピーされません。新しArray 内の参照は、元の Array 内の参照と同じオブジェクト指します

対照的にArray詳細コピーでは、要素自体および要素直接的または間接的に参照するすべての対象コピーされます。

クローンは、元の Array と同じ Type です。

このメソッドは O(n) 操作です。ここで、nLength です。

使用例使用例

次のコード例では、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 -.
*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

Array.Clone メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Array.Clone メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS