SortedList.CopyTo(Array, Int32) 메서드

정의

배열의 지정된 인덱스에서 SortedList 시작하여 요소를 1차원 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
Public Overridable Sub CopyTo (array As Array, arrayIndex As Integer)

매개 변수

array
Array

에서 복사SortedList한 개체의 대상인 DictionaryEntry 1차원 Array 개체입니다. Array 0부터 시작하는 인덱싱이 있어야 합니다.

arrayIndex
Int32

복사가 시작되는 인덱 array 스(0부터 시작)입니다.

구현

예외

arraynull입니다.

arrayIndex가 0보다 작습니다.

array 가 다차원입니다.

-또는-

원본 SortedList 개체의 요소 수가 대상array의 끝 부분에 arrayIndex 있는 사용 가능한 공간보다 큰 경우

원본 SortedList 의 형식을 대상 array의 형식으로 자동으로 캐스팅할 수 없습니다.

예제

다음 코드 예제에서는 개체의 값을 1 SortedList 차원 Array 개체로 복사하는 방법을 보여줍니다.

 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.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


   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

End Class


'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

설명

키/값 쌍은 열거자가 개체를 반복하는 순서와 동일한 순서로 개체에 SortedList 복사 Array 됩니다.

에서 키만 복사하려면 .를 SortedList사용합니다 SortedList.Keys.CopyTo.

의 값만 복사하려면 .를 SortedList사용합니다 SortedList.Values.CopyTo.

이 메서드는 O(n) 작업입니다 nCount.

적용 대상

추가 정보