ArrayList.Add(Object) ArrayList.Add(Object) ArrayList.Add(Object) ArrayList.Add(Object) Method

정의

개체를 ArrayList의 끝 부분에 추가합니다.Adds an object to the end of the ArrayList.

public:
 virtual int Add(System::Object ^ value);
public virtual int Add (object value);
abstract member Add : obj -> int
override this.Add : obj -> int
Public Overridable Function Add (value As Object) As Integer

매개 변수

value
Object Object Object Object

Object의 끝에 추가할 ArrayList입니다.The Object to be added to the end of the ArrayList. 값은 null이 될 수 있습니다.The value can be null.

반환

value가 추가된 ArrayList 인덱스입니다.The ArrayList index at which the value has been added.

구현

예외

ArrayList이 읽기 전용인 경우The ArrayList is read-only.

또는-or- ArrayList가 고정 크기입니다.The ArrayList has a fixed size.

예제

다음 코드 예제에서는 요소를 추가 하는 방법을 보여 줍니다는 ArrayList합니다.The following code example shows how to add elements to the ArrayList.

using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myList, char mySeparator );
int main()
{
   
   // Creates and initializes a new ArrayList.
   ArrayList^ myAL = gcnew ArrayList;
   myAL->Add( "The" );
   myAL->Add( "quick" );
   myAL->Add( "brown" );
   myAL->Add( "fox" );
   
   // Creates and initializes a new Queue.
   Queue^ myQueue = gcnew Queue;
   myQueue->Enqueue( "jumps" );
   myQueue->Enqueue( "over" );
   myQueue->Enqueue( "the" );
   myQueue->Enqueue( "lazy" );
   myQueue->Enqueue( "dog" );
   
   // Displays the ArrayList and the Queue.
   Console::WriteLine( "The ArrayList initially contains the following:" );
   PrintValues( myAL, '\t' );
   Console::WriteLine( "The Queue initially contains the following:" );
   PrintValues( myQueue, '\t' );
   
   // Copies the Queue elements to the end of the ArrayList.
   myAL->AddRange( myQueue );
   
   // Displays the ArrayList.
   Console::WriteLine( "The ArrayList now contains the following:" );
   PrintValues( myAL, '\t' );
}

void PrintValues( IEnumerable^ myList, char mySeparator )
{
   IEnumerator^ myEnum = myList->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      Console::Write( "{0}{1}", mySeparator, obj );
   }

   Console::WriteLine();
}

/* 
This code produces the following output.

The ArrayList initially contains the following:
    The    quick    brown    fox
The Queue initially contains the following:
    jumps    over    the    lazy    dog
The ArrayList now contains the following:
    The    quick    brown    fox    jumps    over    the    lazy    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" );

      // Creates and initializes a new Queue.
      Queue myQueue = new Queue();
      myQueue.Enqueue( "jumps" );
      myQueue.Enqueue( "over" );
      myQueue.Enqueue( "the" );
      myQueue.Enqueue( "lazy" );
      myQueue.Enqueue( "dog" );

      // Displays the ArrayList and the Queue.
      Console.WriteLine( "The ArrayList initially contains the following:" );
      PrintValues( myAL, '\t' );
      Console.WriteLine( "The Queue initially contains the following:" );
      PrintValues( myQueue, '\t' );

      // Copies the Queue elements to the end of the ArrayList.
      myAL.AddRange( myQueue );

      // Displays the ArrayList.
      Console.WriteLine( "The ArrayList now contains the following:" );
      PrintValues( myAL, '\t' );
   }

   public static void PrintValues( IEnumerable myList, char mySeparator )  {
      foreach ( Object obj in myList )
         Console.Write( "{0}{1}", mySeparator, obj );
      Console.WriteLine();
   }

}


/* 
This code produces the following output.

The ArrayList initially contains the following:
    The    quick    brown    fox
The Queue initially contains the following:
    jumps    over    the    lazy    dog
The ArrayList now contains the following:
    The    quick    brown    fox    jumps    over    the    lazy    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")
        
        ' Creates and initializes a new Queue.
        Dim myQueue As New Queue()
        myQueue.Enqueue("jumps")
        myQueue.Enqueue("over")
        myQueue.Enqueue("the")
        myQueue.Enqueue("lazy")
        myQueue.Enqueue("dog")
        
        ' Displays the ArrayList and the Queue.
        Console.WriteLine("The ArrayList initially contains the following:")
        PrintValues(myAL, ControlChars.Tab)
        Console.WriteLine("The Queue initially contains the following:")
        PrintValues(myQueue, ControlChars.Tab)
        
        ' Copies the Queue elements to the end of the ArrayList.
        myAL.AddRange(myQueue)
        
        ' Displays the ArrayList.
        Console.WriteLine("The ArrayList now contains the following:")
        PrintValues(myAL, ControlChars.Tab)
    End Sub

    Public Shared Sub PrintValues(myList As IEnumerable, mySeparator As Char)
        Dim obj As [Object]
        For Each obj In  myList
          Console.Write( "{0}{1}", mySeparator, obj )
        Next obj
        Console.WriteLine()
    End Sub

End Class


' This code produces the following output.
' 
' The ArrayList initially contains the following:
'     The    quick    brown    fox
' The Queue initially contains the following:
'     jumps    over    the    lazy    dog
' The ArrayList now contains the following:
'     The    quick    brown    fox    jumps    over    the    lazy    dog 

설명

ArrayList 허용 null 유효한 값으로 중복 요소를 허용 합니다.ArrayList accepts null as a valid value and allows duplicate elements.

경우 Count 이미 같은 Capacity의 용량을 ArrayList 자동으로 내부 배열을 다시 할당 하 여 증가 하는 새 요소가 추가 되기 전에 기존 요소를 새 배열에 복사 됩니다.If Count already equals Capacity, the capacity of the ArrayList is increased by automatically reallocating the internal array, and the existing elements are copied to the new array before the new element is added.

하는 경우 Count 는 보다 작은 Capacity,이 메서드는 o (1) 작업입니다.If Count is less than Capacity, this method is an O(1) operation. 이 메서드는 O가 용량을 늘려야 하는 새 요소를 수용할 수 있도록 하는 경우 (n) 작업, 여기서 nCount합니다.If the capacity needs to be increased to accommodate the new element, this method becomes an O(n) operation, where n is Count.

적용 대상

추가 정보