ArrayList.ToArray 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將 的 ArrayList 元素複製到新的陣列。
多載
| 名稱 | Description |
|---|---|
| ToArray() | |
| ToArray(Type) |
將 ArrayList 的項目複製到指定之項目類型的新陣列。 |
ToArray()
public:
virtual cli::array <System::Object ^> ^ ToArray();
public virtual object[] ToArray();
abstract member ToArray : unit -> obj[]
override this.ToArray : unit -> obj[]
Public Overridable Function ToArray () As Object()
傳回
Object[]
一個 Object 包含元素副本 ArrayList的陣列。
備註
元素的複製方式為 Array.Copy,這是一個 O(n) 運算,其中 n 為 Count。
適用於
ToArray(Type)
將 ArrayList 的項目複製到指定之項目類型的新陣列。
public:
virtual Array ^ ToArray(Type ^ type);
public virtual Array ToArray(Type type);
abstract member ToArray : Type -> Array
override this.ToArray : Type -> Array
Public Overridable Function ToArray (type As Type) As Array
參數
傳回
一個包含 ArrayList元素副本的指定元素類型的陣列。
例外狀況
type 是 null。
來源的型別 ArrayList 無法自動轉換為指定的類型。
範例
以下複製範例展示了如何將 的 ArrayList 元素複製到 字串陣列。
using System;
using System.Collections;
public class SamplesArrayList {
public static void Main() {
// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add( "The" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );
myAL.Add( "jumps" );
myAL.Add( "over" );
myAL.Add( "the" );
myAL.Add( "lazy" );
myAL.Add( "dog" );
// Displays the values of the ArrayList.
Console.WriteLine( "The ArrayList contains the following values:" );
PrintIndexAndValues( myAL );
// Copies the elements of the ArrayList to a string array.
String[] myArr = (String[]) myAL.ToArray( typeof( string ) );
// Displays the contents of the string array.
Console.WriteLine( "The string array contains the following values:" );
PrintIndexAndValues( myArr );
}
public static void PrintIndexAndValues( ArrayList myList ) {
int i = 0;
foreach ( Object o in myList )
Console.WriteLine( "\t[{0}]:\t{1}", i++, o );
Console.WriteLine();
}
public static void PrintIndexAndValues( String[] myArr ) {
for ( int i = 0; i < myArr.Length; i++ )
Console.WriteLine( "\t[{0}]:\t{1}", i, myArr[i] );
Console.WriteLine();
}
}
/*
This code produces the following output.
The ArrayList contains the following values:
[0]: The
[1]: quick
[2]: brown
[3]: fox
[4]: jumps
[5]: over
[6]: the
[7]: lazy
[8]: dog
The string array contains the following values:
[0]: The
[1]: quick
[2]: brown
[3]: fox
[4]: jumps
[5]: over
[6]: the
[7]: lazy
[8]: dog
*/
Imports System.Collections
Public Class SamplesArrayList
Public Shared Sub Main()
' Creates and initializes a new ArrayList.
Dim myAL As New ArrayList()
myAL.Add("The")
myAL.Add("quick")
myAL.Add("brown")
myAL.Add("fox")
myAL.Add("jumps")
myAL.Add("over")
myAL.Add("the")
myAL.Add("lazy")
myAL.Add("dog")
' Displays the values of the ArrayList.
Console.WriteLine("The ArrayList contains the following values:")
PrintIndexAndValues(myAL)
' Copies the elements of the ArrayList to a string array.
Dim myArr As String() = CType(myAL.ToArray(GetType(String)), String())
' Displays the contents of the string array.
Console.WriteLine("The string array contains the following values:")
PrintIndexAndValues(myArr)
End Sub
Overloads Public Shared Sub PrintIndexAndValues(myList As ArrayList)
Dim i As Integer = 0
Dim o As [Object]
For Each o In myList
Console.WriteLine(" [{0}]: {1}", i, o)
i = i + 1
Next o
Console.WriteLine()
End Sub
Overloads Public Shared Sub PrintIndexAndValues(myArr() As String)
Dim i As Integer
For i = 0 To myArr.Length - 1
Console.WriteLine(" [{0}]: {1}", i, myArr(i))
Next i
Console.WriteLine()
End Sub
End Class
'This code produces the following output.
'
'The ArrayList contains the following values:
' [0]: The
' [1]: quick
' [2]: brown
' [3]: fox
' [4]: jumps
' [5]: over
' [6]: the
' [7]: lazy
' [8]: dog
'
'The string array contains the following values:
' [0]: The
' [1]: quick
' [2]: brown
' [3]: fox
' [4]: jumps
' [5]: over
' [6]: the
' [7]: lazy
' [8]: dog
備註
物件中 ArrayList 的所有物件都會被鑄造成 Type 參數中 type 指定的條件。
元素的複製方式為 Array.Copy,這是一個 O(n) 運算,其中 n 為 Count。