BitArray 全体を互換性のある 1 次元の Array にコピーします。コピー操作は、コピー先の配列の指定したインデックスから始まります。
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
BitArray から要素がコピーされる 1 次元の Array 。 Array には、0 から始まるインデックス番号が必要です。 - index
コピーの開始位置となる、array の 0 から始まるインデックス番号。
実装
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | array が null 参照 (Visual Basic では Nothing) です。 |
ArgumentOutOfRangeException | index が 0 未満です。 |
ArgumentException | array が多次元です。
または index が array の長さ以上です。 または コピー元の BitArray の要素数が、index からコピー先の array の末尾までに格納できる数を超えています。 |
InvalidCastException | コピー元の BitArray の型が、コピー先の array の型に自動的にキャストできません。 |
解説
互換性のある型の配列を指定する必要があります。サポートされている配列型は、 bool 、 int 、および byte だけです。
このメソッドは、 Array.Copy を使用して要素をコピーします。
使用例
[Visual Basic, C#, C++] BitArray を 1 次元の Array にコピーする方法の例を次に示します。
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic
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 As Array = Array.CreateInstance(GetType(Boolean), 8)
myBoolArray.SetValue(False, 0)
myBoolArray.SetValue(False, 1)
' Displays the values of the target Array.
Console.WriteLine("The target Boolean Array contains the " _
+ "following (before and after copying):")
PrintValues(myBoolArray, ControlChars.Tab)
' 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, ControlChars.Tab)
' Creates and initializes the one-dimensional target Array of type integer.
Dim myIntArray As Array = Array.CreateInstance(GetType(Integer), 8)
myIntArray.SetValue(42, 0)
myIntArray.SetValue(43, 1)
' Displays the values of the target Array.
Console.WriteLine("The target Boolean Array contains the " _
+ "following (before and after copying):")
PrintValues(myIntArray, ControlChars.Tab)
' 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, ControlChars.Tab)
' Creates and initializes the one-dimensional target Array of type integer.
Dim myByteArray As Array = Array.CreateInstance(GetType(Byte), 8)
myByteArray.SetValue(CByte(10), 0)
myByteArray.SetValue(CByte(11), 1)
' Displays the values of the target Array.
Console.WriteLine("The target Boolean Array contains the " _
+ "following (before and after copying):")
PrintValues(myByteArray, ControlChars.Tab)
' 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, ControlChars.Tab)
' 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
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 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 copyto on bit arrays are bool[], int[] and byte[].
' at System.Collections.BitArray.CopyTo(Array array, Int32 index) in <UnknownName>:line 0
' at SamplesBitArray.Main()
[C#]
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.
Array myBoolArray = Array.CreateInstance( typeof(bool), 8 );
myBoolArray.SetValue( false, 0 );
myBoolArray.SetValue( false, 1 );
// Displays the values of the target Array.
Console.WriteLine( "The target Boolean Array contains the following (before and after copying):" );
PrintValues( myBoolArray, '\t' );
// 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, '\t' );
// Creates and initializes the one-dimensional target Array of type integer.
Array myIntArray = Array.CreateInstance( typeof(int), 8 );
myIntArray.SetValue( 42, 0 );
myIntArray.SetValue( 43, 1 );
// Displays the values of the target Array.
Console.WriteLine( "The target Boolean Array contains the following (before and after copying):" );
PrintValues( myIntArray, '\t' );
// 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, '\t' );
// 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, '\t' );
// 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, '\t' );
// 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( 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 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 copyto on bit arrays are bool[], int[] and byte[].
at System.Collections.BitArray.CopyTo(Array array, Int32 index) in <UnknownName>:line 0
at SamplesBitArray.Main()
*/
[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();
}
int main() {
// Creates and initializes the source BitArray.
const int ArraySize = 4;
BitArray* myBA = new BitArray( ArraySize );
for( int i(0); i < ArraySize; ++i ) {
myBA->Item[i] = true;
}
// Creates and initializes the one-dimensional target Array of type Boolean.
Array* myBoolArray = Array::CreateInstance( __typeof(bool), 8 );
myBoolArray->SetValue( __box(false), 0 );
myBoolArray->SetValue( __box(false), 1 );
// Displays the values of the target Array.
Console::WriteLine( S"The target Boolean Array contains the following (before and after copying):" );
PrintValues( myBoolArray, '\t' );
// 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, '\t' );
// Creates and initializes the one-dimensional target Array of type integer.
Array* myIntArray = Array::CreateInstance( __typeof(int), 8 );
myIntArray->SetValue( __box(42), 0 );
myIntArray->SetValue( __box(43), 1 );
// Displays the values of the target Array.
Console::WriteLine( S"The target Boolean Array contains the following (before and after copying):" );
PrintValues( myIntArray, '\t' );
// 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, '\t' );
// Creates and initializes the one-dimensional target Array of type integer.
Array* myByteArray = Array::CreateInstance( __typeof(Byte), 8 );
myByteArray->SetValue( __box((Byte)10), 0 );
myByteArray->SetValue( __box((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, '\t' );
// 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, '\t' );
// Returns an exception if the array is not of type Boolean, integer or byte.
try {
Array* myStringArray=Array::CreateInstance( __typeof(String), 8 );
myStringArray->SetValue( S"Hello", 0 );
myStringArray->SetValue( S"World", 1 );
myBA->CopyTo( myStringArray, 3 );
} catch ( Exception* myException ) {
Console::WriteLine( String::Concat( S"Exception: ", myException->ToString() ));
}
}
/*
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 main()
*/
[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 ファミリ, .NET Compact Framework - Windows CE .NET
参照
BitArray クラス | BitArray メンバ | System.Collections 名前空間 | Array