ArrayList.Add(Object) メソッド
定義
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
パラメーター
- value
- 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
注釈
ArrayListnull
は、有効な値としてを受け入れ、重複する要素を許可します。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)
は操作になります。ここで、 n
は Count です。If the capacity needs to be increased to accommodate the new element, this method becomes an O(n)
operation, where n
is Count.