Queue.CopyToとは? わかりやすく解説

Queue.CopyTo メソッド

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

Queue要素既存1 次元Arrayコピーしますコピー操作は、配列内の指定したインデックスから始まります

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

Public Sub CopyTo ( _
    array As T(), _
    arrayIndex As Integer _
)
Dim instance As Queue(Of
 T)
Dim array As T()
Dim arrayIndex As Integer

instance.CopyTo(array, arrayIndex)
public void CopyTo (
    T[] array,
    int arrayIndex
)
public:
void CopyTo (
    array<T>^ array, 
    int arrayIndex
)
public void CopyTo (
    T[] array, 
    int arrayIndex
)
public function CopyTo (
    array : T[], 
    arrayIndex : int
)

パラメータ

array

Queue から要素コピーされる 1 次元ArrayArray には、0 から始まるインデックス番号が必要です。

arrayIndex

コピー開始位置となる、array の 0 から始まるインデックス番号

例外例外
例外種類条件

ArgumentNullException

arraynull 参照 (Visual Basic では Nothing) です。

ArgumentOutOfRangeException

index が 0 未満です。

ArgumentException

indexarray長さ上です。

または

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

解説解説

要素は、列挙子が Queue反復処理するのと同じ順序で、Arrayコピーされます。

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

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Queue.CopyTo メソッド

Queue要素既存1 次元Arrayコピーしますコピー操作は、配列内の指定したインデックスから始まります

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

例外例外
例外種類条件

ArgumentNullException

arraynull 参照 (Visual Basic では Nothing) です。

ArgumentOutOfRangeException

index が 0 未満です。

ArgumentException

array多次元です。

または

indexarray長さ上です。

または

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

ArrayTypeMismatchException

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

解説解説

要素は、列挙子が Queue反復処理するのと同じ順序で、Arrayコピーされます。

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

使用例使用例

Queue1 次元配列コピーする方法の例を次に示します

Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesQueue    

    Public Shared Sub Main()

        ' Creates and initializes the source Queue.
        Dim mySourceQ As New
 Queue()
        mySourceQ.Enqueue("three")
        mySourceQ.Enqueue("napping")
        mySourceQ.Enqueue("cats")
        mySourceQ.Enqueue("in")
        mySourceQ.Enqueue("the")
        mySourceQ.Enqueue("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 entire source Queue 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 Queue 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

    Public Shared Sub PrintValues(myArr
 As Array, mySeparator As Char)
        Dim myObj As [Object]
        For Each myObj In
  myArr
            Console.Write("{0}{1}", mySeparator, myObj)
        Next myObj
        Console.WriteLine()
    End Sub 'PrintValues

End Class 'SamplesQueue


' 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

using System;
using System.Collections;
public class SamplesQueue  {

   public static void Main()
  {

      // Creates and initializes the source Queue.
      Queue mySourceQ = new Queue();
      mySourceQ.Enqueue( "three" );
      mySourceQ.Enqueue( "napping" );
      mySourceQ.Enqueue( "cats" );
      mySourceQ.Enqueue( "in" );
      mySourceQ.Enqueue( "the" );
      mySourceQ.Enqueue( "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 entire source Queue 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 Queue 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 )  {
      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

*/ 
using namespace System;
using namespace System::Collections;
void PrintValues( Array^ myArr, char mySeparator
 );
int main()
{
   // Creates and initializes the source Queue.
   Queue^ mySourceQ = gcnew Queue;
   mySourceQ->Enqueue( "three" );
   mySourceQ->Enqueue( "napping" );
   mySourceQ->Enqueue( "cats" );
   mySourceQ->Enqueue( "in" );
   mySourceQ->Enqueue( "the" );
   mySourceQ->Enqueue( "barn" );

   // Creates and initializes the one-dimensional target Array.
   Array^ myTargetArray = Array::CreateInstance( String::typeid, 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 Queue 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 Queue to a new standard array.
   array<Object^>^myStandardArray = mySourceQ->ToArray();

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

void PrintValues( Array^ myArr, char mySeparator
 )
{
   IEnumerator^ myEnum = myArr->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ myObj = safe_cast<Object^>(myEnum->Current);
      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

 */
import System.*;
import System.Collections.*;

public class SamplesQueue
{
    public static void main(String[]
 args)
    {
        // Creates and initializes the source Queue.
        Queue mySourceQ =  new Queue();
        mySourceQ.Enqueue("three");
        mySourceQ.Enqueue("napping");
        mySourceQ.Enqueue("cats");
        mySourceQ.Enqueue("in");
        mySourceQ.Enqueue("the");
        mySourceQ.Enqueue("barn");

        // Creates and initializes the one-dimensional target Array.
        Array myTargetArray = Array.CreateInstance(String.class.ToType(),
 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 Queue 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 Queue 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, ' ');
    } //main

    public static void PrintValues(Array
 myArr, char mySeparator)
    {
        IEnumerator e = myArr.GetEnumerator();
        while(e.MoveNext()) {
            Object myObj = e.get_Current(); 
            Console.Write("{0}{1}", System.Convert.ToString(mySeparator)
,
                System.Convert.ToString( myObj));
        }
        Console.WriteLine();
    } //PrintValues
} //SamplesQueue

/* 
 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
 */
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



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

辞書ショートカット

すべての辞書の索引

Queue.CopyToのお隣キーワード
検索ランキング

   

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



Queue.CopyToのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS