次の方法で共有


Stack.CopyTo メソッド

既存の 1 次元の ArrayStack をコピーします。コピー操作は、配列の指定したインデックスから始まります。

Public Overridable Sub CopyTo( _
   ByVal array As Array, _   ByVal index As Integer _) Implements ICollection.CopyTo
[C#]
public virtual void CopyTo(Arrayarray,intindex);
[C++]
public: virtual void CopyTo(Array* array,intindex);
[JScript]
public function CopyTo(
   array : Array,index : int);

パラメータ

  • array
    Stack から要素がコピーされる 1 次元の ArrayArray には、0 から始まるインデックス番号が必要です。
  • index
    コピーの開始位置となる、array の 0 から始まるインデックス番号。

実装

ICollection.CopyTo

例外

例外の種類 条件
ArgumentNullException array が null 参照 (Visual Basic では Nothing) です。
ArgumentOutOfRangeException index が 0 未満です。
ArgumentException array が多次元です。

または

index が array の長さ以上です。

または

コピー元の Stack の要素数が、index からコピー先の array の末尾までに格納できる数を超えています。

InvalidCastException コピー元の Stack の型が、コピー先の array の型に自動的にキャストできません。

解説

要素は、 Pop の連続的な呼び出しによって返される要素の順序と同様に、後入れ先出しの順序で配列にコピーされます。

使用例

[Visual Basic, C#, C++] Stack を 1 次元の Array のインスタンスにコピーする方法と、1 次元の標準配列にコピーする方法の例を次に示します。

 
Imports System
Imports System.Collections

Public Class SamplesStack    
    
    Public Shared Sub Main()
        
        ' Creates and initializes the source Stack.
        Dim mySourceQ As New Stack()
        mySourceQ.Push("barn")
        mySourceQ.Push("the")
        mySourceQ.Push("in")
        mySourceQ.Push("cats")
        mySourceQ.Push("napping")
        mySourceQ.Push("three")
        
        ' 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 entire source Stack to the target Array, starting
        ' at index 6.
        mySourceQ.CopyTo(myTargetArray, 6)
        
        ' Displays the values of the target Array.
        PrintValues(myTargetArray, " "c)
        
        ' Copies the entire source Stack to a new standard array.
        Dim myStandardArray As Object() = mySourceQ.ToArray()
        
        ' Displays the values of the new standard array.
        Console.WriteLine("The new standard array contains the following:")
        PrintValues(myStandardArray, " "c)
    End Sub    
    
    Overloads 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
    
    Overloads Public Shared Sub PrintValues(myArr() As Object, _
       mySeparator As Char)
       
        Dim myObj As Object
        For Each myObj In  myArr
            Console.Write("{0}{1}", mySeparator, myObj)
        Next myObj
        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 three napping cats in the barn
' The new standard array contains the following:
'  three napping cats in the barn 

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

   public static void Main()  {

      // Creates and initializes the source Stack.
      Stack mySourceQ = new Stack();
      mySourceQ.Push( "barn" );
      mySourceQ.Push( "the" );
      mySourceQ.Push( "in" );
      mySourceQ.Push( "cats" );
      mySourceQ.Push( "napping" );
      mySourceQ.Push( "three" );

      // 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 entire source Stack to the target Array, starting at index 6.
      mySourceQ.CopyTo( myTargetArray, 6 );

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

      // Copies the entire source Stack to a new standard array.
      Object[] myStandardArray = mySourceQ.ToArray();

      // Displays the values of the new standard array.
      Console.WriteLine( "The new standard array contains the following:" );
      PrintValues( myStandardArray, ' ' );

   }


   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();
   }

   public static void PrintValues( Object[] myArr, char mySeparator )  {
      foreach ( Object myObj in myArr )  {
         Console.Write( "{0}{1}", mySeparator, myObj );
      }
      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 three napping cats in the barn   
The new standard array contains the following:
 three napping cats in the barn
*/ 

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::Collections;


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( S"{0}{1}", __box(mySeparator), myEnumerator->Current );
   }
   Console::WriteLine();
}

void PrintValues( Object* myArr __gc[], Char mySeparator )  {
    for( int i(0); i < myArr->Count; ++i ) {
           Console::Write( S"{0}{1}", __box(mySeparator), myArr[i]->ToString() );
   }
   Console::WriteLine();
}


int main()  {

   // Creates and initializes the source Stack.
   Stack* mySourceQ = new Stack();
   mySourceQ->Push( S"barn" );
   mySourceQ->Push( S"the" );
   mySourceQ->Push( S"in" );
   mySourceQ->Push( S"cats" );
   mySourceQ->Push( S"napping" );
   mySourceQ->Push( S"three" );

   // 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 );

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

   // Copies the entire source Stack to the target Array, starting at index 6.
   mySourceQ->CopyTo( myTargetArray, 6 );

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

   // Copies the entire source Stack to a new standard array.
   Object* myStandardArray __gc[] = mySourceQ->ToArray();

   // Displays the values of the new standard array.
   Console::WriteLine( S"The new standard array contains the following:" );
   PrintValues( myStandardArray, ' ' );

}

/*
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 three napping cats in the barn
The new standard array contains the following:
 three napping cats in the barn
*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

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