StringCollection.CopyTo 方法

从目标数组的指定索引处开始,将全部 StringCollection 值复制到一维字符串数组中。

**命名空间:**System.Collections.Specialized
**程序集:**System(在 system.dll 中)

语法

声明
Public Sub CopyTo ( _
    array As String(), _
    index As Integer _
)
用法
Dim instance As StringCollection
Dim array As String()
Dim index As Integer

instance.CopyTo(array, index)
public void CopyTo (
    string[] array,
    int index
)
public:
void CopyTo (
    array<String^>^ array, 
    int index
)
public void CopyTo (
    String[] array, 
    int index
)
public function CopyTo (
    array : String[], 
    index : int
)

参数

  • array
    一维字符串数组,用作从 StringCollection 复制元素的目标数组。Array 必须具有从零开始的索引。
  • index
    array 中从零开始的索引,从此处开始复制。

异常

异常类型 条件

ArgumentNullException

array 为 空引用(在 Visual Basic 中为 Nothing)。

ArgumentOutOfRangeException

index 小于零。

ArgumentException

array 是多维的。

- 或 -

index 等于或大于 array 的长度。

- 或 -

StringCollection 中的元素数目大于从 index 到目标 array 末尾之间的可用空间。

InvalidCastException

StringCollection 的类型无法自动转换为目标 array 的类型。

备注

指定的数组必须是兼容类型的数组。

元素将按照 StringCollection 的枚举数循环访问 StringCollection 的相同顺序复制到 Array 中。

此方法的运算复杂度为 O(n),其中 n 是 Count

示例

下面的代码示例将 StringCollection 复制到数组。

Imports System
Imports System.Collections
Imports System.Collections.Specialized

Public Class SamplesStringCollection   

   Public Shared Sub Main()

      ' Creates and initializes a new StringCollection.
      Dim myCol As New StringCollection()
      Dim myArr() As [String] = {"RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED"}
      myCol.AddRange(myArr)

      Console.WriteLine("Initial contents of the StringCollection:")
      PrintValues(myCol)

      ' Copies the collection to a new array starting at index 0.
      Dim myArr2(myCol.Count) As [String]
      myCol.CopyTo(myArr2, 0)

      Console.WriteLine("The new array contains:")
      Dim i As Integer
      For i = 0 To myArr2.Length - 1
         Console.WriteLine("   [{0}] {1}", i, myArr2(i))
      Next i
      Console.WriteLine()

   End Sub 'Main

   Public Shared Sub PrintValues(myCol As IEnumerable)
      Dim obj As [Object]
      For Each obj In  myCol
         Console.WriteLine("   {0}", obj)
      Next obj
      Console.WriteLine()
   End Sub 'PrintValues

End Class 'SamplesStringCollection 


'This code produces the following output.
'
'Initial contents of the StringCollection:
'   RED
'   orange
'   yellow
'   RED
'   green
'   blue
'   RED
'   indigo
'   violet
'   RED
'
'The new array contains:
'   [0] RED
'   [1] orange
'   [2] yellow
'   [3] RED
'   [4] green
'   [5] blue
'   [6] RED
'   [7] indigo
'   [8] violet
'   [9] RED
'
using System;
using System.Collections;
using System.Collections.Specialized;

public class SamplesStringCollection  {

   public static void Main()  {

      // Creates and initializes a new StringCollection.
      StringCollection myCol = new StringCollection();
      String[] myArr = new String[] { "RED", "orange", "yellow", "RED", "green", "blue", "RED", "indigo", "violet", "RED" };
      myCol.AddRange( myArr );

      Console.WriteLine( "Initial contents of the StringCollection:" );
      PrintValues( myCol );

      // Copies the collection to a new array starting at index 0.
      String[] myArr2 = new String[myCol.Count];
      myCol.CopyTo( myArr2, 0 );

      Console.WriteLine( "The new array contains:" );
      for ( int i = 0; i < myArr2.Length; i++ )  {
         Console.WriteLine( "   [{0}] {1}", i, myArr2[i] );
      }
      Console.WriteLine();

   }

   public static void PrintValues( IEnumerable myCol )  {
      foreach ( Object obj in myCol )
         Console.WriteLine( "   {0}", obj );
      Console.WriteLine();
   }

}

/*
This code produces the following output.

Initial contents of the StringCollection:
   RED
   orange
   yellow
   RED
   green
   blue
   RED
   indigo
   violet
   RED

The new array contains:
   [0] RED
   [1] orange
   [2] yellow
   [3] RED
   [4] green
   [5] blue
   [6] RED
   [7] indigo
   [8] violet
   [9] RED

*/
#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void PrintValues( IEnumerable^ myCol );
int main()
{
   
   // Creates and initializes a new StringCollection.
   StringCollection^ myCol = gcnew StringCollection;
   array<String^>^myArr = {"RED","orange","yellow","RED","green","blue","RED","indigo","violet","RED"};
   myCol->AddRange( myArr );
   Console::WriteLine( "Initial contents of the StringCollection:" );
   PrintValues( myCol );
   
   // Copies the collection to a new array starting at index 0.
   array<String^>^myArr2 = gcnew array<String^>(myCol->Count);
   myCol->CopyTo( myArr2, 0 );
   Console::WriteLine( "The new array contains:" );
   for ( int i = 0; i < myArr2->Length; i++ )
   {
      Console::WriteLine( "   [{0}] {1}", i, myArr2[ i ] );

   }
   Console::WriteLine();
}

void PrintValues( IEnumerable^ myCol )
{
   IEnumerator^ myEnum = myCol->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      Console::WriteLine( "   {0}", obj );
   }

   Console::WriteLine();
}

/*
This code produces the following output.

Initial contents of the StringCollection:
   RED
   orange
   yellow
   RED
   green
   blue
   RED
   indigo
   violet
   RED

The new array contains:
   [0] RED
   [1] orange
   [2] yellow
   [3] RED
   [4] green
   [5] blue
   [6] RED
   [7] indigo
   [8] violet
   [9] RED

*/
import System.* ;
import System.Collections.* ;
import System.Collections.Specialized.* ;

public class SamplesStringCollection
{    
    public static void main(String[] args)
    {        
        // Creates and initializes a new StringCollection.
        StringCollection myCol =  new StringCollection();
        String myArr[] = new String[] { "RED", "orange", "yellow", "RED",
            "green", "blue", "RED", "indigo", "violet", "RED" };
        myCol.AddRange(myArr);
        
        Console.WriteLine("Initial contents of the StringCollection:");
        PrintValues(myCol);
        
        // Copies the collection to a new array starting at index 0.
        String myArr2[] = new String[myCol.get_Count()];
        myCol.CopyTo(myArr2, 0);
        
        Console.WriteLine("The new array contains:");
        for (int i=0; i < myArr2.length; i++) {        
            Console.WriteLine("   [{0}] {1}",System.Convert.ToString(i),
                myArr2.get_Item(i));
        } 
        Console.WriteLine();
    } //main

    public static void PrintValues(IEnumerable myCol)
    {
        Object obj = null;
        IEnumerator objEnum = myCol.GetEnumerator();

        while (objEnum.MoveNext()) {        
            obj = objEnum.get_Current();
            Console.WriteLine("   {0}", obj);
        }

        Console.WriteLine();
    } //PrintValues
} //SamplesStringCollection 

/*
This code produces the following output.

Initial contents of the StringCollection:
   RED
   orange
   yellow
   RED
   green
   blue
   RED
   indigo
   violet
   RED

The new array contains:
   [0] RED
   [1] orange
   [2] yellow
   [3] RED
   [4] green
   [5] blue
   [6] RED
   [7] indigo
   [8] violet
   [9] RED

*/

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

StringCollection 类
StringCollection 成员
System.Collections.Specialized 命名空间
Array 类
GetEnumerator