ArrayList.Add(Object) Metodo

Definizione

Aggiunge un oggetto alla fine di ArrayList.

public:
 virtual int Add(System::Object ^ value);
public virtual int Add (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

Parametri

value
Object

Object da aggiungere alla fine della classe ArrayList. Il valore può essere null.

Restituisce

Indice ArrayList in corrispondenza del quale è stato aggiunto value.

Implementazioni

Eccezioni

La classe ArrayList è di sola lettura.

-oppure-

Le dimensioni dell'oggetto ArrayList sono fisse.

Esempio

Nell'esempio di codice seguente viene illustrato come aggiungere elementi a 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

Commenti

ArrayList accetta null come valore valido e consente elementi duplicati.

Se Count è già uguale a Capacity, la capacità di viene aumentata riallocazione automatica della ArrayList matrice interna e gli elementi esistenti vengono copiati nella nuova matrice prima dell'aggiunta del nuovo elemento.

Se Count è minore di Capacity, questo metodo è un'operazione O(1) . Se la capacità deve essere aumentata per contenere il nuovo elemento, questo metodo diventa un'operazione O(n) , dove n è Count.

Si applica a

Vedi anche