ArrayList.ToArray Method (Type)
.NET Framework 2.0
Copies the elements of the ArrayList to a new array of the specified element type.
Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)
Assembly: mscorlib (in mscorlib.dll)
public Array ToArray ( Type type )
public function ToArray ( type : Type ) : Array
Not applicable.
Parameters
- type
The element Type of the destination array to create and copy elements to.
Return Value
An array of the specified element type containing copies of the elements of the ArrayList.All of the objects in the ArrayList object will be cast to the Type specified in the type parameter.
The elements are copied using System.Array.Copy, which is an O(n) operation, where n is Count.
The following copy example shows how to copy the elements of an ArrayList to a string array.
using namespace System; using namespace System::Collections; void PrintIndexAndValues( ArrayList^ myList ); void PrintIndexAndValues( array<String^>^myArr ); int main() { // Creates and initializes a new ArrayList. ArrayList^ myAL = gcnew ArrayList; myAL->Add( "The" ); myAL->Add( "quick" ); myAL->Add( "brown" ); myAL->Add( "fox" ); myAL->Add( "jumped" ); 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. array<String^>^myArr = reinterpret_cast<array<String^>^>(myAL->ToArray( String::typeid )); // Displays the contents of the string array. Console::WriteLine( "The string array contains the following values:" ); PrintIndexAndValues( myArr ); } void PrintIndexAndValues( ArrayList^ myList ) { int i = 0; IEnumerator^ myEnum = myList->GetEnumerator(); while ( myEnum->MoveNext() ) { Object^ o = safe_cast<Object^>(myEnum->Current); Console::WriteLine( "\t[{0}]:\t{1}", i++, o ); } Console::WriteLine(); } void PrintIndexAndValues( array<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]: jumped [5]: over [6]: the [7]: lazy [8]: dog The string array contains the following values: [0]: The [1]: quick [2]: brown [3]: fox [4]: jumped [5]: over [6]: the [7]: lazy [8]: dog */
import System.*;
import System.Collections.*;
public class SamplesArrayList
{
public static void main(String[] args)
{
// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add("The");
myAL.Add("quick");
myAL.Add("brown");
myAL.Add("fox");
myAL.Add("jumped");
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(String.class.ToType())));
// Displays the contents of the string array.
Console.WriteLine("The string array contains the following values:");
PrintIndexAndValues(myArr);
} //main
public static void PrintIndexAndValues(ArrayList myList)
{
int i = 0;
for (int iCtr = 0; iCtr < myList.get_Count(); iCtr++) {
Object o = myList.get_Item(iCtr);
Console.WriteLine("\t[{0}]:\t{1}", System.Convert.ToString(i++),
System.Convert.ToString(o));
}
Console.WriteLine();
} //PrintIndexAndValues
public static void PrintIndexAndValues(String[] myArr)
{
for (int i = 0; i < myArr.get_Length(); i++) {
Console.WriteLine("\t[{0}]:\t{1}", System.Convert.ToString(i),
System.Convert.ToString(myArr.get_Item(i)));
}
Console.WriteLine();
} //PrintIndexAndValues
} //SamplesArrayList
/*
This code produces the following output.
The ArrayList contains the following values:
[0]: The
[1]: quick
[2]: brown
[3]: fox
[4]: jumped
[5]: over
[6]: the
[7]: lazy
[8]: dog
The string array contains the following values:
[0]: The
[1]: quick
[2]: brown
[3]: fox
[4]: jumped
[5]: over
[6]: the
[7]: lazy
[8]: dog
*/
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.Community Additions
ADD
Show: