語言

List<T>.CopyTo 方法

定義

將其中或部分複製 List<T> 到陣列。

多載

名稱 Description
CopyTo(T[], Int32)

將整個 List<T> 資料複製到相容的一維陣列,從目標陣列指定的索引開始。

CopyTo(Int32, T[], Int32, Int32)

從目標陣列指定的索引開始,將一系列元素 List<T> 複製到相容的一維陣列。

CopyTo(T[])

從目標陣列的起始處,將整個 List<T> 資料複製到相容的一維陣列。

範例

以下範例展示了該 CopyTo 方法的三種超載方式。 List<T> A字串被建立並填充5個字串。 建立一個包含 15 個元素的空字串陣列,並 CopyTo(T[]) 利用方法過載將清單中的所有元素從陣列的第一個元素開始複製到陣列。 CopyTo(T[], Int32)方法過載用於將清單中所有元素複製到從陣列索引 6 開始的陣列(索引 5 為空)。 最後, CopyTo(Int32, T[], Int32, Int32) 方法過載用於從索引 2 開始的 3 個元素複製到從陣列索引 12 開始的陣列(索引 11 為空)。 陣列內容隨後會被顯示出來。

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        List<string> dinosaurs = new List<string>();

        dinosaurs.Add("Tyrannosaurus");
        dinosaurs.Add("Amargasaurus");
        dinosaurs.Add("Mamenchisaurus");
        dinosaurs.Add("Brachiosaurus");
        dinosaurs.Add("Compsognathus");

        Console.WriteLine();
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }

        // Declare an array with 15 elements.
        string[] array = new string[15];

        dinosaurs.CopyTo(array);
        dinosaurs.CopyTo(array, 6);
        dinosaurs.CopyTo(2, array, 12, 3);

        Console.WriteLine("\nContents of the array:");
        foreach(string dinosaur in array)
        {
            Console.WriteLine(dinosaur);
        }
    }
}

/* This code example produces the following output:

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Compsognathus

Contents of the array:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Compsognathus

Tyrannosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Compsognathus

Mamenchisaurus
Brachiosaurus
Compsognathus
 */
Imports System.Collections.Generic

Public Class Example

    Public Shared Sub Main()

        Dim dinosaurs As New List(Of String)

        dinosaurs.Add("Tyrannosaurus")
        dinosaurs.Add("Amargasaurus")
        dinosaurs.Add("Mamenchisaurus")
        dinosaurs.Add("Brachiosaurus")
        dinosaurs.Add("Compsognathus")

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        ' Declare an array with 15 elements (0 through 14).
        Dim array(14) As String

        dinosaurs.CopyTo(array)
        dinosaurs.CopyTo(array, 6)
        dinosaurs.CopyTo(2, array, 12, 3)

        Console.WriteLine(vbLf & "Contents of the array:")
        For Each dinosaur As String In array
            Console.WriteLine(dinosaur)
        Next

    End Sub
End Class

' This code example produces the following output:
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Compsognathus
'
'Contents of the array:
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Compsognathus
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Compsognathus
'
'Mamenchisaurus
'Brachiosaurus
'Compsognathus

CopyTo(T[], Int32)

來源:
List.cs
來源:
List.cs
來源:
List.cs
來源:
List.cs
來源:
List.cs

將整個 List<T> 資料複製到相容的一維陣列,從目標陣列指定的索引開始。

public:
 virtual void CopyTo(cli::array <T> ^ array, int arrayIndex);
public void CopyTo(T[] array, int arrayIndex);
abstract member CopyTo : 'T[] * int -> unit
override this.CopyTo : 'T[] * int -> unit
Public Sub CopyTo (array As T(), arrayIndex As Integer)

參數

array
T[]

一維 Array ,即從 中複製 List<T>元素的目的地。 必須 Array 有零基索引。

arrayIndex
Int32

複製開始的零基索引 array 。

實作

例外狀況

array 是 null。

arrayIndex 小於0。

來源 List<T> 中的元素數量大於從 arrayIndex 目的地 array到末端的可用空間。

備註

此方法用於 Array.Copy 複製元素。

元素的 Array 複製順序與列舉器遍歷 List<T>的順序相同。

此方法為 O(n) 運算,其中 n 為 Count。

適用於

CopyTo(Int32, T[], Int32, Int32)

來源:
List.cs
來源:
List.cs
來源:
List.cs
來源:
List.cs
來源:
List.cs

從目標陣列指定的索引開始,將一系列元素 List<T> 複製到相容的一維陣列。

public:
 void CopyTo(int index, cli::array <T> ^ array, int arrayIndex, int count);
public void CopyTo(int index, T[] array, int arrayIndex, int count);
member this.CopyTo : int * 'T[] * int * int -> unit
Public Sub CopyTo (index As Integer, array As T(), arrayIndex As Integer, count As Integer)

參數

index
Int32

複製開始的來源 List<T> 中以零為基礎的索引。

array
T[]

一維 Array ,即從 中複製 List<T>元素的目的地。 必須 Array 有零基索引。

arrayIndex
Int32

複製開始的零基索引 array 。

count
Int32

要複製的項目數目。

例外狀況

array 是 null。

index 小於0。

-或-

arrayIndex 小於0。

-或-

count 小於0。

index 等於或大於 Count 源 List<T>的 。

-或-

從 index 來源 List<T> 到端點的元素數大於從 arrayIndex 目的 array端到端的可用空間。

備註

此方法用於 Array.Copy 複製元素。

元素的 Array 複製順序與列舉器遍歷 List<T>的順序相同。

此方法為 O(n) 運算,其中 n 為 count。

適用於

CopyTo(T[])

來源:
List.cs
來源:
List.cs
來源:
List.cs
來源:
List.cs
來源:
List.cs

從目標陣列的起始處,將整個 List<T> 資料複製到相容的一維陣列。

public:
 void CopyTo(cli::array <T> ^ array);
public void CopyTo(T[] array);
member this.CopyTo : 'T[] -> unit
Public Sub CopyTo (array As T())

參數

array
T[]

一維 Array ,即從 中複製 List<T>元素的目的地。 必須 Array 有零基索引。

例外狀況

array 是 null。

來源 List<T> 中的元素數量多於目的 array 節點能包含的元素數。

備註

此方法用於 Array.Copy 複製元素。

元素的 Array 複製順序與列舉器遍歷 List<T>的順序相同。

此方法為 O(n) 運算,其中 n 為 Count。

適用於