英語で読む

次の方法で共有


ArrayList.SetRange(Int32, ICollection) メソッド

定義

コレクションの要素を ArrayList 内の要素の範囲にコピーします。

C#
public virtual void SetRange (int index, System.Collections.ICollection c);

パラメーター

index
Int32

c の要素のコピーを開始する位置を示す、0 から始まる ArrayList インデックス。

c
ICollection

ArrayList にコピーする要素を持つ ICollection。 コレクション自体を null にすることはできませんが、null である要素を格納することは可能です。

例外

index が 0 未満です。

- または -

indexc 内の要素数を加算すると Count より大きい値になります。

cnullです。

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" );
      myAL.Add( "jumps" );
      myAL.Add( "over" );
      myAL.Add( "the" );
      myAL.Add( "lazy" );
      myAL.Add( "dog" );

      // Creates and initializes the source ICollection.
      Queue mySourceList = new Queue();
      mySourceList.Enqueue( "big" );
      mySourceList.Enqueue( "gray" );
      mySourceList.Enqueue( "wolf" );

      // Displays the values of five elements starting at index 0.
      ArrayList mySubAL = myAL.GetRange( 0, 5 );
      Console.WriteLine( "Index 0 through 4 contains:" );
      PrintValues( mySubAL, '\t' );

      // Replaces the values of five elements starting at index 1 with the values in the ICollection.
      myAL.SetRange( 1, mySourceList );

      // Displays the values of five elements starting at index 0.
      mySubAL = myAL.GetRange( 0, 5 );
      Console.WriteLine( "Index 0 through 4 now contains:" );
      PrintValues( mySubAL, '\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.

Index 0 through 4 contains:
        The     quick   brown   fox     jumps
Index 0 through 4 now contains:
        The     big     gray    wolf    jumps
*/

注釈

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

ICollection の要素の順序は、 に ArrayList保持されます。

このメソッドは 操作です 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

こちらもご覧ください