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

BitArray.CopyTo メソッド

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

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

例外例外
例外の種類条件

ArgumentNullException

array が null 参照 (Visual Basic では Nothing) です。

ArgumentOutOfRangeException

index が 0 未満です。

ArgumentException

array が多次元です。

または

index が array の長さ以上です。

または

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

InvalidCastException

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

解説解説

互換性のある型の配列を指定する必要があります。サポートされている配列型は、bool、int、および byte だけです。

このメソッドは、Array.Copy を使用して要素をコピーします。

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

使用例使用例

BitArray を 1 次元の Array にコピーする方法を次のコード例に示します。

Imports System
Imports System.Collections

Public Class SamplesBitArray

    Public Shared Sub Main()

        ' Creates and initializes the source BitArray.
        Dim myBA As New
 BitArray(4)
        myBA(0) = True
        myBA(1) = True
        myBA(2) = True
        myBA(3) = True

        ' Creates and initializes the one-dimensional target Array of
 type Boolean.
        Dim myBoolArray(7) As Boolean
        myBoolArray(0) = False
        myBoolArray(1) = False

        ' Displays the values of the target Array.
        Console.WriteLine("The target Boolean Array contains the
 following (before and after copying):")
        PrintValues(myBoolArray)

        ' Copies the entire source BitArray to the target BitArray,
 starting at index 3.
        myBA.CopyTo(myBoolArray, 3)

        ' Displays the values of the target Array.
        PrintValues(myBoolArray)

        ' Creates and initializes the one-dimensional target Array of
 type integer.
        Dim myIntArray(7) As Integer
        myIntArray(0) = 42
        myIntArray(1) = 43

        ' Displays the values of the target Array.
        Console.WriteLine("The target Boolean Array contains the
 following (before and after copying):")
        PrintValues(myIntArray)

        ' Copies the entire source BitArray to the target BitArray,
 starting at index 3.
        myBA.CopyTo(myIntArray, 3)

        ' Displays the values of the target Array.
        PrintValues(myIntArray)

        ' Creates and initializes the one-dimensional target Array of
 type integer.
        Dim myByteArray As Array = Array.CreateInstance(GetType(Byte),
 8)
        myByteArray.SetValue(System.Convert.ToByte(10), 0)
        myByteArray.SetValue(System.Convert.ToByte(11), 1)

        ' Displays the values of the target Array.
        Console.WriteLine("The target Boolean Array contains the
 following (before and after copying):")
        PrintValues(myByteArray)

        ' Copies the entire source BitArray to the target BitArray,
 starting at index 3.
        myBA.CopyTo(myByteArray, 3)

        ' Displays the values of the target Array.
        PrintValues(myByteArray)

        ' Returns an exception if the array is not of type Boolean,
 integer or byte.
        Try
            Dim myStringArray As Array = Array.CreateInstance(GetType(String),
 8)
            myStringArray.SetValue("Hello", 0)
            myStringArray.SetValue("World", 1)
            myBA.CopyTo(myStringArray, 3)
        Catch myException As Exception
            Console.WriteLine("Exception: " + myException.ToString())
        End Try

    End Sub 'Main

    Public Shared Sub PrintValues(myArr
 As IEnumerable)
        Dim obj As [Object]
        For Each obj In
  myArr
            Console.Write("{0,8}", obj)
        Next obj
        Console.WriteLine()
    End Sub 'PrintValues

End Class 'SamplesBitArray 


' This code produces the following output.
' 
' The target Boolean Array contains the following (before and after
 copying):
'    False   False   False   False   False   False   False   False
'    False   False   False    True    True    True    True   False
' The target Boolean Array contains the following (before and after
 copying):
'       42      43       0       0       0       0       0       0
'       42      43       0      15       0       0       0       0
' The target Boolean Array contains the following (before and after
 copying):
'       10      11       0       0       0       0       0       0
'       10      11       0      15       0       0       0       0
' Exception: System.ArgumentException: Only supported array types for
 CopyTo on BitArrays are Boolean[], Int32[] and Byte[].
'    at System.Collections.BitArray.CopyTo(Array array, Int32 index)
'    at SamplesBitArray.Main()

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

   public static void Main()
  {

      // Creates and initializes the source BitArray.
      BitArray myBA = new BitArray( 4 );
      myBA[0] = myBA[1] = myBA[2] = myBA[3] = true;

      // Creates and initializes the one-dimensional target Array of
 type Boolean.
      bool[] myBoolArray = new bool[8];
      myBoolArray[0] = false;
      myBoolArray[1] = false;

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

      // Copies the entire source BitArray to the target BitArray, starting
 at index 3.
      myBA.CopyTo( myBoolArray, 3 );

      // Displays the values of the target Array.
      PrintValues( myBoolArray );

      // Creates and initializes the one-dimensional target Array of
 type integer.
      int[] myIntArray = new int[8];
      myIntArray[0] = 42;
      myIntArray[1] = 43;

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

      // Copies the entire source BitArray to the target BitArray, starting
 at index 3.
      myBA.CopyTo( myIntArray, 3 );

      // Displays the values of the target Array.
      PrintValues( myIntArray );

      // Creates and initializes the one-dimensional target Array of
 type integer.
      Array myByteArray = Array.CreateInstance( typeof(byte), 8 );
      myByteArray.SetValue( (byte) 10, 0 );
      myByteArray.SetValue( (byte) 11, 1 );

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

      // Copies the entire source BitArray to the target BitArray, starting
 at index 3.
      myBA.CopyTo( myByteArray, 3 );

      // Displays the values of the target Array.
      PrintValues( myByteArray );

      // Returns an exception if the array is not of type Boolean, integer
 or byte.
      try  {
         Array myStringArray=Array.CreateInstance( typeof(String), 8 );
         myStringArray.SetValue( "Hello", 0 );
         myStringArray.SetValue( "World", 1 );
         myBA.CopyTo( myStringArray, 3 );
      } catch ( Exception myException )  {
         Console.WriteLine("Exception: " + myException.ToString());
      }
   }

   public static void PrintValues(
 IEnumerable myArr )  {
      foreach ( Object obj in myArr ) {
         Console.Write( "{0,8}", obj );
      }
      Console.WriteLine();
   }

}


/* 
This code produces the following output.

The target Boolean Array contains the following (before and after copying):
   False   False   False   False   False   False   False   False
   False   False   False    True    True    True    True   False
The target Boolean Array contains the following (before and after copying):
      42      43       0       0       0       0       0       0
      42      43       0      15       0       0       0       0
The target Boolean Array contains the following (before and after copying):
      10      11       0       0       0       0       0       0
      10      11       0      15       0       0       0       0
Exception: System.ArgumentException: Only supported array types for
 CopyTo on BitArrays are Boolean[], Int32[] and Byte[].
   at System.Collections.BitArray.CopyTo(Array array, Int32 index)
   at SamplesBitArray.Main()

*/
using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myArr );
int main()
{
   // Creates and initializes the source BitArray.
   BitArray^ myBA = gcnew BitArray( 4 );
   myBA[ 0 ] = true;
   myBA[ 1 ] = true;
   myBA[ 2 ] = true;
   myBA[ 3 ] = true;

   // Creates and initializes the one-dimensional target Array of type
 Boolean.
   array<Boolean>^myBoolArray = gcnew array<Boolean>(8);
   myBoolArray[ 0 ] = false;
   myBoolArray[ 1 ] = false;

   // Displays the values of the target Array.
   Console::WriteLine( "The target Boolean Array contains the following (before
 and after copying):" );
   PrintValues( dynamic_cast<IEnumerable^>(myBoolArray) );

   // Copies the entire source BitArray to the target BitArray, starting
 at index 3.
   myBA->CopyTo( myBoolArray, 3 );

   // Displays the values of the target Array.
   PrintValues( dynamic_cast<IEnumerable^>(myBoolArray) );

   // Creates and initializes the one-dimensional target Array of type
 integer.
   array<Int32>^myIntArray = gcnew array<Int32>(8);
   myIntArray[ 0 ] = 42;
   myIntArray[ 1 ] = 43;

   // Displays the values of the target Array.
   Console::WriteLine( "The target Boolean Array contains the following (before
 and after copying):" );
   PrintValues( dynamic_cast<IEnumerable^>(myIntArray) );

   // Copies the entire source BitArray to the target BitArray, starting
 at index 3.
   myBA->CopyTo( myIntArray, 3 );

   // Displays the values of the target Array.
   PrintValues( dynamic_cast<IEnumerable^>(myIntArray) );

   // Creates and initializes the one-dimensional target Array of type
 integer.
   Array^ myByteArray = Array::CreateInstance( Byte::typeid, 8 );
   myByteArray->SetValue( (Byte)10, 0 );
   myByteArray->SetValue( (Byte)11, 1 );

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

   // Copies the entire source BitArray to the target BitArray, starting
 at index 3.
   myBA->CopyTo( myByteArray, 3 );

   // Displays the values of the target Array.
   PrintValues( myByteArray );

   // Returns an exception if the array is not of type Boolean, integer
 or byte.
   try
   {
      Array^ myStringArray = Array::CreateInstance( String::typeid, 8 );
      myStringArray->SetValue( "Hello", 0 );
      myStringArray->SetValue( "World", 1 );
      myBA->CopyTo( myStringArray, 3 );
   }
   catch ( Exception^ myException ) 
   {
      Console::WriteLine( "Exception: {0}", myException );
   }

}

void PrintValues( IEnumerable^ myArr )
{
   IEnumerator^ myEnum = myArr->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      Console::Write( "{0,8}", obj );
   }

   Console::WriteLine();
}

/* 
 This code produces the following output.
 
 The target Boolean Array contains the following (before and after copying):
    False   False   False   False   False   False   False   False
    False   False   False    True    True    True    True   False
 The target Boolean Array contains the following (before and after copying):
       42      43       0       0       0       0       0       0
       42      43       0      15       0       0       0       0
 The target Boolean Array contains the following (before and after copying):
       10      11       0       0       0       0       0       0
       10      11       0      15       0       0       0       0
 Exception: System.ArgumentException: Only supported array types for
 CopyTo on BitArrays are Boolean[], Int32[] and Byte[].
    at System.Collections.BitArray.CopyTo(Array array, Int32 index)
    at SamplesBitArray.Main()

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

public class SamplesBitArray
{
    public static void main(String[]
 args)
    {
        // Creates and initializes the source BitArray.
        BitArray myBA = new BitArray(4);

        myBA.set_Item(0, true);
        myBA.set_Item(1, true);
        myBA.set_Item(2, true);
        myBA.set_Item(3, true);

        // Creates and initializes the one-dimensional target Array
 of 
        // type Boolean.
        boolean myBoolArray[] = new boolean[8];
        myBoolArray[0] = false;
        myBoolArray[1] = false;

        // Displays the values of the target Array.
        Console.WriteLine("The target Boolean Array contains the following"
            + " (before and after copying):");
        PrintValues(myBoolArray);

        // Copies the entire source BitArray to the target BitArray,
 starting 
        // at index 3.
        myBA.CopyTo(myBoolArray, 3);

        // Displays the values of the target Array.
        PrintValues(myBoolArray);

        // Creates and initializes the one-dimensional target Array
 of type 
        // integer.
        int myIntArray[] = new int[8];
        myIntArray[0] = 42;
        myIntArray[1] = 43;

        // Displays the values of the target Array.
        Console.WriteLine("The target Boolean Array contains the following"
            + " (before and after copying):");
        PrintValues(myIntArray);

        // Copies the entire source BitArray to the target BitArray,
 starting 
        // at index 3.
        myBA.CopyTo(myIntArray, 3);

        // Displays the values of the target Array.
        PrintValues(myIntArray);

        // Creates and initializes the one-dimensional target Array
 of 
        // type integer.
        Array myByteArray = Array.CreateInstance(ubyte.class.ToType(),
 8);
        myByteArray.SetValue((System.Byte)(10), 0);
        myByteArray.SetValue((System.Byte)(11), 1);

        // Displays the values of the target Array.
        Console.WriteLine("The target Boolean Array contains the following"
            + " (before and after copying):");
        PrintValues(myByteArray);

        // Copies the entire source BitArray to the target BitArray,
 starting
        // at index 3.
        myBA.CopyTo(myByteArray, 3);

        // Displays the values of the target Array.
        PrintValues(myByteArray);

        // Returns an exception if the array is not of type Boolean,
 integer
        // or byte.
        try {
            Array myStringArray = Array.CreateInstance(String.class.ToType()
,8);
            myStringArray.SetValue("Hello", 0);
            myStringArray.SetValue("World", 1);
            myBA.CopyTo(myStringArray, 3);
        }
        catch (System.Exception myException) {
            Console.WriteLine(("Exception: " + myException.ToString()));
        }
    } //main

    public static void PrintValues(IEnumerable
 myArr)
    {
        IEnumerator objMyEnum = myArr.GetEnumerator();
        while (objMyEnum.MoveNext()) {
            Object obj = objMyEnum.get_Current();
            Console.Write("{0,8}", obj);
        }
        Console.WriteLine();
    } //PrintValues
}//SamplesBitArray 

/* 
 This code produces the following output.
 
 The target Boolean Array contains the following (before and after copying):
    False   False   False   False   False   False   False   False
    False   False   False    True    True    True    True   False
 The target Boolean Array contains the following (before and after copying):
       42      43       0       0       0       0       0       0
       42      43       0      15       0       0       0       0
 The target Boolean Array contains the following (before and after copying):
       10      11       0       0       0       0       0       0
       10      11       0      15       0       0       0       0
 Exception: System.ArgumentException: Only supported array types for
 CopyTo on
 BitArrays are Boolean[], Int32[] and Byte[].
    at System.Collections.BitArray.CopyTo(Array array, Int32 index)
    at SamplesBitArray.main(String[] args)

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


このページでは「.NET Framework クラス ライブラリ リファレンス」からBitArray.CopyTo メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からBitArray.CopyTo メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からBitArray.CopyTo メソッド を検索

英和和英テキスト翻訳

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

辞書ショートカット

すべての辞書の索引

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

   

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



BitArray.CopyTo メソッドのページの著作権

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

©2026 GRAS Group, Inc.RSS