ArrayList.ToArray ArrayList.ToArray ArrayList.ToArray ArrayList.ToArray Method

Definizione

Copia gli elementi di ArrayList in una nuova matrice.Copies the elements of the ArrayList to a new array.

Overload

ToArray() ToArray() ToArray() ToArray()

Copia gli elementi di ArrayList in una nuova matrice Object.Copies the elements of the ArrayList to a new Object array.

ToArray(Type) ToArray(Type) ToArray(Type)

Copia gli elementi di ArrayList in una nuova matrice del tipo di elemento specificato.Copies the elements of the ArrayList to a new array of the specified element type.

ToArray() ToArray() ToArray() ToArray()

Copia gli elementi di ArrayList in una nuova matrice Object.Copies the elements of the ArrayList to a new Object array.

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()

Restituisce

Object[]

Matrice Object contenente le copie degli elementi di ArrayList.An Object array containing copies of the elements of the ArrayList.

Commenti

Gli elementi vengono copiati mediante Array.Copy, che è un'operazione O (n) operazione, in cui n è Count.The elements are copied using Array.Copy, which is an O(n) operation, where n is Count.

ToArray(Type) ToArray(Type) ToArray(Type)

Copia gli elementi di ArrayList in una nuova matrice del tipo di elemento specificato.Copies the elements of the ArrayList to a new array of the specified element type.

public:
 virtual Array ^ ToArray(Type ^ type);
public virtual Array ToArray (Type type);
abstract member ToArray : Type -> Array
override this.ToArray : Type -> Array

Parametri

type
Type Type Type Type

Elemento Type della matrice di destinazione da creare e in cui copiare gli elementi.The element Type of the destination array to create and copy elements to.

Restituisce

Matrice del tipo di elemento specificato, contenente copie degli elementi di ArrayList.An array of the specified element type containing copies of the elements of the ArrayList.

Eccezioni

Non è possibile eseguire automaticamente il cast del tipo dell'oggetto ArrayList di origine al tipo specificato.The type of the source ArrayList cannot be cast automatically to the specified type.

Esempi

L'esempio copia seguente illustra come copiare gli elementi di un ArrayList a una matrice di stringhe.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( "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.
   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]:    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

*/
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
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 'Main

    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 'PrintIndexAndValues

    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 'PrintIndexAndValues

End Class 'SamplesArrayList


'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

Commenti

Tutti gli oggetti nel ArrayList oggetto sarà possibile eseguire il cast di Type specificato nel type parametro.All of the objects in the ArrayList object will be cast to the Type specified in the type parameter.

Gli elementi vengono copiati mediante Array.Copy, che è un'operazione O (n) operazione, in cui n è Count.The elements are copied using Array.Copy, which is an O(n) operation, where n is Count.

Vedi anche

Si applica a