ArrayList.ToArray 메서드

정의

ArrayList의 요소를 새 배열에 복사합니다.

오버로드

ToArray()

ArrayList의 요소를 새 Object 배열에 복사합니다.

ToArray(Type)

ArrayList의 요소를 지정된 요소 형식의 새 배열에 복사합니다.

ToArray()

Source:
ArrayList.cs
Source:
ArrayList.cs
Source:
ArrayList.cs

ArrayList의 요소를 새 Object 배열에 복사합니다.

public:
 virtual cli::array <System::Object ^> ^ ToArray();
public virtual object[] ToArray ();
public virtual object?[] ToArray ();
abstract member ToArray : unit -> obj[]
override this.ToArray : unit -> obj[]
Public Overridable Function ToArray () As Object()

반환

Object[]

ArrayList의 요소 복사본을 포함하는 Object 배열입니다.

설명

요소는 연산인 를 사용하여 Array.Copy복사됩니다 O(n) . 여기서 n 은 입니다 Count.

적용 대상

ToArray(Type)

Source:
ArrayList.cs
Source:
ArrayList.cs
Source:
ArrayList.cs

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

매개 변수

type
Type

요소를 복사해 넣기 위해 새로 만들 대상 배열의 Type 요소입니다.

반환

ArrayList의 요소 복사본을 포함하는 지정된 요소 형식의 배열입니다.

예외

type이(가) null인 경우

소스 ArrayList의 형식을 지정된 형식으로 자동 캐스팅할 수 없습니다.

예제

다음 복사 예제에서는 의 요소를 ArrayList 문자열 배열에 복사하는 방법을 보여줍니다.

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.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.

추가 정보

적용 대상