SortedList.CopyTo(Array, Int32) 方法

定義

從陣列中指定的索引處開始,將 SortedList 元素複製到一維 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

一維 Array 物件,其為從 DictionaryEntry 複製之 SortedList 物件的目的地。 Array 必須有以零為起始的索引。

arrayIndex
Int32

array 中以零起始的索引,即開始複製的位置。

實作

例外狀況

arraynull

arrayIndex 小於零。

array 是多維的。

-或-

來源 SortedList 物件中的元素數目,大於從 arrayIndex 到目的 array 結尾的可用空間。

來源 SortedList 的類型無法自動轉換成目的 array 的類型。

範例

下列程式代碼範例示範如何將 物件中的 SortedList 值複製到一維 Array 物件。

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

適用於

另請參閱