次の方法で共有


ArrayList.CopyTo メソッド

ArrayList またはその一部を 1 次元配列にコピーします。

オーバーロードの一覧

ArrayList 全体を互換性のある 1 次元の Array にコピーします。コピー操作は、コピー先の配列の先頭から始まります。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Overridable Sub CopyTo(Array)

[C#] public virtual void CopyTo(Array);

[C++] public: virtual void CopyTo(Array*);

[JScript] public function CopyTo(Array);

ArrayList 全体を互換性のある 1 次元の Array にコピーします。コピー操作は、コピー先の配列の指定したインデックスから始まります。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Overridable Sub CopyTo(Array, Integer) Implements ICollection.CopyTo

[C#] public virtual void CopyTo(Array, int);

[C++] public: virtual void CopyTo(Array*, int);

[JScript] public function CopyTo(Array, int);

要素の範囲を ArrayList から互換性のある 1 次元の Array にコピーします。コピー操作は、コピー先の配列の指定したインデックスから始まります。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Overridable Sub CopyTo(Integer, Array, Integer, Integer)

[C#] public virtual void CopyTo(int, Array, int, int);

[C++] public: virtual void CopyTo(int, Array*, int, int);

[JScript] public function CopyTo(int, Array, int, int);

使用例

ArrayList を 1 次元の System.Array にコピーする方法の例を次に示します。

 
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesArrayList    
    
    Public Shared Sub Main()
        
        ' Creates and initializes the source ArrayList.
        Dim mySourceList As New ArrayList()
        mySourceList.Add("three")
        mySourceList.Add("napping")
        mySourceList.Add("cats")
        mySourceList.Add("in")
        mySourceList.Add("the")
        mySourceList.Add("barn")
        
        ' Creates and initializes the one-dimensional target Array.
        Dim myTargetArray As Array = Array.CreateInstance(GetType(String), 15)
        myTargetArray.SetValue("The", 0)
        myTargetArray.SetValue("quick", 1)
        myTargetArray.SetValue("brown", 2)
        myTargetArray.SetValue("fox", 3)
        myTargetArray.SetValue("jumped", 4)
        myTargetArray.SetValue("over", 5)
        myTargetArray.SetValue("the", 6)
        myTargetArray.SetValue("lazy", 7)
        myTargetArray.SetValue("dog", 8)
        
        ' Displays the values of the target Array.
        Console.WriteLine("The target Array contains the following " _
           + "(before and after copying):")
        PrintValues(myTargetArray, " "c)
        
        ' Copies the second element from the source ArrayList to the target
        ' ArrayList, starting at index 7.
        mySourceList.CopyTo(1, myTargetArray, 7, 1)
        
        ' Displays the values of the target Array.
        PrintValues(myTargetArray, " "c)
        
        ' Copies the entire source ArrayList to the target ArrayList, starting
        ' at index 6.
        mySourceList.CopyTo(myTargetArray, 6)
        
        ' Displays the values of the target Array.
        PrintValues(myTargetArray, " "c)
        
        ' Copies the entire source ArrayList to the target ArrayList, starting
        ' at index 0.
        mySourceList.CopyTo(myTargetArray)
        
        ' Displays the values of the target Array.
        PrintValues(myTargetArray, " "c)
    End Sub 'Main
    
    
    Public Shared Sub PrintValues(myArr As Array, mySeparator As Char)
        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("{0}{1}", mySeparator, myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' The target Array contains the following (before and after copying):
'  The quick brown fox jumped over the lazy dog      
'  The quick brown fox jumped over the napping dog      
'  The quick brown fox jumped over three napping cats in the barn   
'  three napping cats in the barn three napping cats in the barn 

[C#] 
using System;
using System.Collections;
public class SamplesArrayList  {

   public static void Main()  {

      // Creates and initializes the source ArrayList.
      ArrayList mySourceList = new ArrayList();
      mySourceList.Add( "three" );
      mySourceList.Add( "napping" );
      mySourceList.Add( "cats" );
      mySourceList.Add( "in" );
      mySourceList.Add( "the" );
      mySourceList.Add( "barn" );

      // Creates and initializes the one-dimensional target Array.
      Array myTargetArray=Array.CreateInstance( typeof(String), 15 );
      myTargetArray.SetValue( "The", 0 );
      myTargetArray.SetValue( "quick", 1 );
      myTargetArray.SetValue( "brown", 2 );
      myTargetArray.SetValue( "fox", 3 );
      myTargetArray.SetValue( "jumped", 4 );
      myTargetArray.SetValue( "over", 5 );
      myTargetArray.SetValue( "the", 6 );
      myTargetArray.SetValue( "lazy", 7 );
      myTargetArray.SetValue( "dog", 8 );

      // Displays the values of the target Array.
      Console.WriteLine( "The target Array contains the following (before and after copying):" );
      PrintValues( myTargetArray, ' ' );

      // Copies the second element from the source ArrayList to the target ArrayList, starting at index 7.
      mySourceList.CopyTo( 1, myTargetArray, 7, 1 );

      // Displays the values of the target Array.
      PrintValues( myTargetArray, ' ' );

      // Copies the entire source ArrayList to the target ArrayList, starting at index 6.
      mySourceList.CopyTo( myTargetArray, 6 );

      // Displays the values of the target Array.
      PrintValues( myTargetArray, ' ' );

      // Copies the entire source ArrayList to the target ArrayList, starting at index 0.
      mySourceList.CopyTo( myTargetArray );

      // Displays the values of the target Array.
      PrintValues( myTargetArray, ' ' );
   }

   public static void PrintValues( Array myArr, char mySeparator )  {
      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( "{0}{1}", mySeparator, myEnumerator.Current );
      }
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

The target Array contains the following (before and after copying):
 The quick brown fox jumped over the lazy dog      
 The quick brown fox jumped over the napping dog      
 The quick brown fox jumped over three napping cats in the barn   
 three napping cats in the barn three napping cats in the barn
*/ 

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;

void PrintValues( Array* myArr, String* mySeparator );
 
void main()  {
 
       // Creates and initializes the source ArrayList.
       ArrayList* mySourceList = new ArrayList();
       mySourceList->Add( S"three" );
       mySourceList->Add( S"napping" );
       mySourceList->Add( S"cats" );
       mySourceList->Add( S"in" );
       mySourceList->Add( S"the" );
       mySourceList->Add( S"barn" );
 
       // Creates and initializes the one-dimensional target Array.
       Array* myTargetArray = Array::CreateInstance( __typeof(String), 15 );
       myTargetArray->SetValue( S"The", 0 );
       myTargetArray->SetValue( S"quick", 1 );
       myTargetArray->SetValue( S"brown", 2 );
       myTargetArray->SetValue( S"fox", 3 );
       myTargetArray->SetValue( S"jumped", 4 );
       myTargetArray->SetValue( S"over", 5 );
       myTargetArray->SetValue( S"the", 6 );
       myTargetArray->SetValue( S"lazy", 7 );
       myTargetArray->SetValue( S"dog", 8 );

 
       String* cSeparator = " ";

       // Displays the values of the target Array.
       Console::WriteLine( "The target Array instance contains the following (before and after copying):" );
       PrintValues( myTargetArray, cSeparator );
 
       // Copies the second element from the source ArrayList to the target ArrayList, starting at index 7.
       mySourceList->CopyTo( 1, myTargetArray, 7, 1 );
 
       // Displays the values of the target Array.
       PrintValues( myTargetArray, cSeparator );
 
       // Copies the entire source ArrayList to the target ArrayList, starting at index 6.
       mySourceList->CopyTo( myTargetArray, 6 );
 
       // Displays the values of the target Array.
       PrintValues( myTargetArray, cSeparator );
 
       // Copies the entire source ArrayList to the target ArrayList, starting at index 0.
       mySourceList->CopyTo( myTargetArray );
 
       // Displays the values of the target Array.
       PrintValues( myTargetArray, cSeparator );
    }
 
void PrintValues( Array* myArr, String* mySeparator )  {
       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( "{0}{1}", mySeparator, myEnumerator->Current );
       }
       Console::WriteLine();
    }

 /* 
 This code produces the following output.
 
 The target Array instance contains the following (before and after copying):
  The quick brown fox jumped over the lazy dog      
  The quick brown fox jumped over the napping dog      
  The quick brown fox jumped over three napping cats in the barn   
  three napping cats in the barn three napping cats in the barn
 */ 

[JScript] 
import System;
import System.Collections;


// Creates and initializes the source ArrayList.
var mySourceList : ArrayList  = new ArrayList();
mySourceList.Add( "three" );
mySourceList.Add( "napping" );
mySourceList.Add( "cats" );
mySourceList.Add( "in" );
mySourceList.Add( "the" );
mySourceList.Add( "barn" );

// Creates and initializes the one-dimensional target Array.
var myTargetArray : System.Array = System.Array.CreateInstance( System.String, 15 );
myTargetArray.SetValue( "The", 0 );
myTargetArray.SetValue( "quick", 1 );
myTargetArray.SetValue( "brown", 2 );
myTargetArray.SetValue( "fox", 3 );
myTargetArray.SetValue( "jumped", 4 );
myTargetArray.SetValue( "over", 5 );
myTargetArray.SetValue( "the", 6 );
myTargetArray.SetValue( "lazy", 7 );
myTargetArray.SetValue( "dog", 8 );

// Displays the values of the target Array.
Console.WriteLine( "The target Array contains the following (before and after copying):" );
PrintValues( myTargetArray, ' ' );

// Copies the second element from the source ArrayList to the target ArrayList, starting at index 7.
mySourceList.CopyTo( 1, myTargetArray, 7, 1 );

// Displays the values of the target Array.
PrintValues( myTargetArray, ' ' );

// Copies the entire source ArrayList to the target ArrayList, starting at index 6.
mySourceList.CopyTo( myTargetArray, 6 );

// Displays the values of the target Array.
PrintValues( myTargetArray, ' ' );

// Copies the entire source ArrayList to the target ArrayList, starting at index 0.
mySourceList.CopyTo( myTargetArray );

// Displays the values of the target Array.
PrintValues( myTargetArray, ' ' );

 
function PrintValues( myArr : System.Array , mySeparator : char  )  {
   var myEnumerator : System.Collections.IEnumerator = myArr.GetEnumerator();
   var i : int= 0;
   var cols : int = myArr.GetLength( myArr.Rank - 1 );
   while ( myEnumerator.MoveNext() )  {
      if ( i < cols )  {
         i++;
      } else  {
         Console.WriteLine();
         i = 1;
      }
      Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
   }
   Console.WriteLine();
}
 /* 
 This code produces the following output.
 
 The target Array contains the following (before and after copying):
  The quick brown fox jumped over the lazy dog      
  The quick brown fox jumped over the napping dog      
  The quick brown fox jumped over three napping cats in the barn   
  three napping cats in the barn three napping cats in the barn
 */ 

参照

ArrayList クラス | ArrayList メンバ | System.Collections 名前空間