BitArray.CopyTo(Array, Int32) 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
public:
virtual void CopyTo(Array ^ array, int index);
public void CopyTo (Array array, int index);
abstract member CopyTo : Array * int -> unit
override this.CopyTo : Array * int -> unit
Public Sub CopyTo (array As Array, index As Integer)
매개 변수
- index
- Int32
array
에서 복사가 시작되는 0부터 시작하는 인덱스입니다.
구현
예외
array
이(가) null
인 경우
index
가 0보다 작은 경우
소스 BitArray의 형식을 대상 array
의 형식으로 자동 캐스팅할 수 없습니다.
예제
다음 코드 예제에서는 을 1차원 Array로 복사 BitArray 하는 방법을 보여줍니다.
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 integer 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 byte.
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 byte 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 integer 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 byte 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 integer 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 byte.
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 byte 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 integer 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 byte 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, int index)
at SamplesBitArray.Main()
*/
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 integer 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 byte.
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 byte 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
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
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 integer 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 byte 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()
설명
지정된 된 배열의 호환 되는 형식 이어야 합니다. , int
및 byte
배열 형식만 bool
지원됩니다.
이 메서드는 를 사용하여 Array.Copy 요소를 복사합니다.
이 메서드는 작업입니다 O(n)
. 여기서 n
은 입니다 Count.
적용 대상
추가 정보
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET