SortedList.CopyTo(Array, Int32) SortedList.CopyTo(Array, Int32) SortedList.CopyTo(Array, Int32) Method

정의

SortedList 요소를 지정한 배열 인덱스에서 시작하여 1차원 Array 개체에 복사합니다.Copies SortedList elements to a one-dimensional Array object, starting at the specified index in the array.

public:
 virtual void CopyTo(Array ^ array, int arrayIndex);
public virtual void CopyTo (Array array, int arrayIndex);
abstract member CopyTo : Array * int -> unit
override this.CopyTo : Array * int -> unit

매개 변수

array
Array Array Array

Array에서 복사한 DictionaryEntry 개체의 대상인 1차원 SortedList 개체입니다.The one-dimensional Array object that is the destination of the DictionaryEntry objects copied from SortedList. Array에는 0부터 시작하는 인덱스가 있어야 합니다.The Array must have zero-based indexing.

arrayIndex
Int32 Int32 Int32

array에서 복사가 시작되는 0부터 시작하는 인덱스입니다.The zero-based index in array at which copying begins.

구현

예외

arraynull입니다.array is null.

arrayIndex가 0보다 작습니다.arrayIndex is less than zero.

array가 다차원 배열인 경우array is multidimensional.

또는-or- 소스 SortedList 개체의 요소 수가 arrayIndex에서 대상 array 끝까지 사용 가능한 공간보다 큰 경우The number of elements in the source SortedList object is greater than the available space from arrayIndex to the end of the destination array.

소스 SortedList의 형식을 대상 array의 형식으로 자동 캐스팅할 수 없습니다.The type of the source SortedList cannot be cast automatically to the type of the destination array.

예제

다음 코드 예제에서 값을 복사 하는 방법을 보여 줍니다는 SortedList 개체를 1 차원으로 Array 개체입니다.The following code example shows how to copy the values in a SortedList object into a one-dimensional Array object.

using namespace System;
using namespace System::Collections;
void PrintValues( array<DictionaryEntry>^ myArr, Char mySeparator );
int main()
{
   
   // Creates and initializes the source SortedList.
   SortedList^ mySourceList = gcnew SortedList;
   mySourceList->Add( 2, "cats" );
   mySourceList->Add( 3, "in" );
   mySourceList->Add( 1, "napping" );
   mySourceList->Add( 4, "the" );
   mySourceList->Add( 0, "three" );
   mySourceList->Add( 5, "barn" );
   
   // Creates and initializes the one-dimensional target Array.
   array<String^>^tempArray = {"The","quick","brown","fox","jumps","over","the","lazy","dog"};
   array<DictionaryEntry>^myTargetArray = gcnew array<DictionaryEntry>(15);
   int i = 0;
   IEnumerator^ myEnum = tempArray->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      String^ s = safe_cast<String^>(myEnum->Current);
      myTargetArray[ i ].Key = i;
      myTargetArray[ i ].Value = s;
      i++;
   }

   
   // 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 SortedList to the target SortedList, starting at index 6.
   mySourceList->CopyTo( myTargetArray, 6 );
   
   // Displays the values of the target Array.
   PrintValues( myTargetArray, ' ' );
}

void PrintValues( array<DictionaryEntry>^ myArr, Char mySeparator )
{
   for ( int i = 0; i < myArr->Length; i++ )
      Console::Write( "{0}{1}", mySeparator, myArr[ i ].Value );
   Console::WriteLine();
}

/*
This code produces the following output.
 
The target Array contains the following (before and after copying):
 The quick brown fox jumps over the lazy dog      
 The quick brown fox jumps over three napping cats in the barn

*/
 using System;
 using System.Collections;
 public class SamplesSortedList  {
 
    public static void Main()  {
 
       // Creates and initializes the source SortedList.
       SortedList mySourceList = new SortedList();
       mySourceList.Add( 2, "cats" );
       mySourceList.Add( 3, "in" );
       mySourceList.Add( 1, "napping" );
       mySourceList.Add( 4, "the" );
       mySourceList.Add( 0, "three" );
       mySourceList.Add( 5, "barn" );
 
       // Creates and initializes the one-dimensional target Array.
       String[] tempArray = new String[] { "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog" };
       DictionaryEntry[] myTargetArray = new DictionaryEntry[15];
       int i = 0;
       foreach ( String s in tempArray )  {
          myTargetArray[i].Key = i;
          myTargetArray[i].Value = s;
          i++;
       }

       // 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 SortedList to the target SortedList, starting at index 6.
       mySourceList.CopyTo( myTargetArray, 6 );
 
       // Displays the values of the target Array.
       PrintValues( myTargetArray, ' ' );
    }
 
    public static void PrintValues( DictionaryEntry[] myArr, char mySeparator )  {
       for ( int i = 0; i < myArr.Length; i++ )
          Console.Write( "{0}{1}", mySeparator, myArr[i].Value );
       Console.WriteLine();
    }

 }


/*
This code produces the following output.
 
The target Array contains the following (before and after copying):
 The quick brown fox jumps over the lazy dog      
 The quick brown fox jumps over three napping cats in the barn

*/ 
Imports System
Imports System.Collections

Public Class SamplesSortedList

   Public Shared Sub Main()

      ' Creates and initializes the source SortedList.
      Dim mySourceList As New SortedList()
      mySourceList.Add(2, "cats")
      mySourceList.Add(3, "in")
      mySourceList.Add(1, "napping")
      mySourceList.Add(4, "the")
      mySourceList.Add(0, "three")
      mySourceList.Add(5, "barn")

      ' Creates and initializes the one-dimensional target Array.
      Dim tempArray() As String = {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"}
      Dim myTargetArray(14) As DictionaryEntry
      Dim i As Integer = 0
      Dim s As String
      For Each s In  tempArray
         myTargetArray(i).Key = i
         myTargetArray(i).Value = s
         i += 1
      Next s

      ' 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 SortedList to the target SortedList, starting at index 6.
      mySourceList.CopyTo(myTargetArray, 6)

      ' Displays the values of the target Array.
      PrintValues(myTargetArray, " "c)

   End Sub 'Main


   Public Shared Sub PrintValues(myArr() As DictionaryEntry, mySeparator As Char)
      Dim i As Integer
      For i = 0 To myArr.Length - 1
         Console.Write("{0}{1}", mySeparator, myArr(i).Value)
      Next i
      Console.WriteLine()
  End Sub 'PrintValues

End Class 'SamplesSortedList 


'This code produces the following output.
' 
'The target Array contains the following (before and after copying):
' The quick brown fox jumps over the lazy dog      
' The quick brown fox jumps over three napping cats in the barn

설명

키/값 쌍을 복사할 합니다 Array 열거자가 반복는 동일한 순서로 개체는 SortedList 개체입니다.The key/value pairs are copied to the Array object in the same order in which the enumerator iterates through the SortedList object.

에 있는 키를 복사 하는 SortedList를 사용 하 여 SortedList.Keys.CopyTo입니다.To copy only the keys in the SortedList, use SortedList.Keys.CopyTo.

값만 복사 하는 SortedList를 사용 하 여 SortedList.Values.CopyTo입니다.To copy only the values in the SortedList, use SortedList.Values.CopyTo.

이 메서드는 O (n) 작업, 여기서 nCount합니다.This method is an O(n) operation, where n is Count.

적용 대상

추가 정보