ArrayList.Add(Object) 메서드

정의

의 끝에 개체를 추가합니다 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 의 끝에 추가할 입니다ArrayList. 값은 .일 null수 있습니다.

반품

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

구현

예외

읽기 ArrayList 전용입니다.

-또는-

크기가 ArrayList 고정되어 있습니다.

예제

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

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

설명

ArrayListnull 유효한 값으로 허용되고 중복 요소를 허용합니다.

이미 같Count으면 Capacity 내부 배열을 자동으로 다시 할당하여 용량 ArrayList 이 증가하고 새 요소가 추가되기 전에 기존 요소가 새 배열에 복사됩니다.

보다 작으면 Count 이 메서드는 작업입니다O(1).Capacity 새 요소를 수용하기 위해 용량을 늘려야 하는 경우 이 메서드는 O(n) 작업이 nCount됩니다.

적용 대상

추가 정보