ArrayList.Add(Object) Método
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Agrega un objeto al final de 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
Parámetros
- value
- Object
Objeto Object que se va a agregar al final de la colección ArrayList. El valor puede ser null
.
Devoluciones
Índice de ArrayList en el que se ha agregado value
.
Implementaciones
Excepciones
Ejemplos
En el ejemplo de código siguiente se muestra cómo agregar elementos 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
Comentarios
ArrayListnull
acepta como un valor válido y permite elementos duplicados.
Si Count ya es Capacityigual a , la capacidad de se incrementa mediante la reasignación automática de ArrayList la matriz interna y los elementos existentes se copian en la nueva matriz antes de agregar el nuevo elemento.
Si Count es menor que Capacity, este método es una O(1)
operación. Si es necesario aumentar la capacidad para acomodar el nuevo elemento, este método se convierte en una O(n)
operación, donde n
es Count.