SortedList.CopyTo(Array, Int32) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将 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对象,它是从中SortedList复制的对象的目标DictionaryEntry。 必须 Array 具有从零开始的索引。
- arrayIndex
- Int32
从零开始复制的索引 array 。
实现
例外
array 是 null。
arrayIndex 小于零。
源 SortedList 的类型不能自动转换为目标 array的类型。
示例
下面的代码示例演示如何将对象中的 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)操作,其中 n 。Count