英語で読む

次の方法で共有


ArrayList.Add(Object) メソッド

定義

ArrayList の末尾にオブジェクトを追加します。

C#
public virtual int Add (object value);
C#
public virtual int Add (object? value);

パラメーター

value
Object

Object の末尾に追加する ArrayList。 値として null を指定できます。

戻り値

value が追加された位置の ArrayList インデックス。

実装

例外

ArrayList は読み取り専用です。

- または -

ArrayList は固定サイズです。

次のコード例は、 に要素を追加する方法を ArrayList示しています。

C#
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
*/

注釈

ArrayListnull 有効な値として受け取り、重複する要素を許可します。

が既に と等しいCapacity場合Count、 のArrayList容量は内部配列を自動的に再割り当てすることで増加し、新しい要素が追加される前に既存の要素が新しい配列にコピーされます。

が 未満の場合Count、このメソッドは操作ですO(1)Capacity 新しい要素に対応するために容量を増やす必要がある場合、このメソッドは 操作になります O(n) 。ここで n 、 は Countです。

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

こちらもご覧ください