다음을 통해 공유


ArrayList.Add 메서드

개체를 ArrayList의 끝 부분에 추가합니다.

네임스페이스: System.Collections
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
Public Overridable Function Add ( _
    value As Object _
) As Integer
‘사용 방법
Dim instance As ArrayList
Dim value As Object
Dim returnValue As Integer

returnValue = instance.Add(value)
public virtual int Add (
    Object value
)
public:
virtual int Add (
    Object^ value
)
public int Add (
    Object value
)
public function Add (
    value : Object
) : int

매개 변수

  • value
    ArrayList의 끝에 추가할 Object입니다. 값은 Null 참조(Visual Basic의 경우 Nothing)이 될 수 있습니다.

반환 값

value가 추가된 ArrayList 인덱스입니다.

예외

예외 형식 조건

NotSupportedException

ArrayList가 읽기 전용인 경우

- 또는 -

ArrayList의 크기가 고정되어 있는 경우

설명

ArrayList은 Null 참조(Visual Basic의 경우 Nothing)을 유효한 값으로 받아들이며 중복 요소를 허용합니다.

Count가 이미 Capacity와 같으면 내부 배열을 자동으로 재할당하여 ArrayList의 용량이 증가하며 기존 요소가 새 배열에 복사된 후 새 요소가 추가됩니다.

CountCapacity보다 작으면 이 메서드는 O(1) 연산입니다. 용량을 증가시켜 새 요소를 수용해야 하는 경우 이 메서드는 O(n) 연산이 됩니다. 여기에서 n은 Count입니다.

예제

다음 코드 예제에서는 ArrayList에 요소를 추가하는 방법을 보여 줍니다.

Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

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("jumped")
        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 'PrintValues

End Class


' This code produces the following output.
' 
' The ArrayList initially contains the following:
'     The    quick    brown    fox
' The Queue initially contains the following:
'     jumped    over    the    lazy    dog
' The ArrayList now contains the following:
'     The    quick    brown    fox    jumped    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( "jumped" );
      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:
    jumped    over    the    lazy    dog
The ArrayList now contains the following:
    The    quick    brown    fox    jumped    over    the    lazy    dog
*/ 
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( "jumped" );
   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:
    jumped    over    the    lazy    dog
The ArrayList now contains the following:
    The    quick    brown    fox    jumped    over    the    lazy    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");

        // Creates and initializes a new Queue.
        Queue myQueue = new Queue();

        myQueue.Enqueue("jumped");
        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');
    } //main

    public static void PrintValues(IEnumerable myList, char mySeparator)
    {
        IEnumerator objMyEnum = myList.GetEnumerator();
        while (objMyEnum.MoveNext()) {
            Object obj = objMyEnum.get_Current();
            Console.Write("{0}{1}",(Char)mySeparator, obj);
        }
        Console.WriteLine();
    } //PrintValues
} //SamplesArrayList 

/* 
 This code produces the following output.
 
 The ArrayList initially contains the following:
     The    quick    brown    fox
 The Queue initially contains the following:
     jumped    over    the    lazy    dog
 The ArrayList now contains the following:
     The    quick    brown    fox    jumped    over    the    lazy    dog
 */
import System;
import System.Collections;


// Creates and initializes a new ArrayList.
var myAL : ArrayList = new ArrayList();
myAL.Add( "The" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );

// Creates and initializes a new Queue.
var myQueue : Queue  = new Queue();
myQueue.Enqueue( "jumped" );
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' );

 
function PrintValues( myList : IEnumerable , mySeparator : char  )  {
   var myEnumerator : System.Collections.IEnumerator  = myList.GetEnumerator();
   while ( myEnumerator.MoveNext() )
      Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
   Console.WriteLine();
}
 /* 
 This code produces the following output.
 
 The ArrayList initially contains the following:
     The    quick    brown    fox
 The Queue initially contains the following:
     jumped    over    the    lazy    dog
 The ArrayList now contains the following:
     The    quick    brown    fox    jumped    over    the    lazy    dog
 */ 

플랫폼

Windows 98, Windows 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

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

ArrayList 클래스
ArrayList 멤버
System.Collections 네임스페이스
AddRange
Insert
Remove
Count